Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [programming-puzzle]

A programming puzzle includes a goal, a partially completed program, and rules outlining how the program can be modified. The program is specifically designed to make achieving the goal difficult. An answer to a programming puzzle takes the program and modifies it only in ways specified in the rules,...

3
votes
2answers
206 views

Algorithm to print the Minimum number of adjacent character swaps necessary to remove all instances of “VK” in a given string?

For example, how many adjacent swaps are at least needed to convert some string such as BVVKCV to one without any instances of VK...
0
votes
0answers
92 views

C# puzzle - execute impossible code [duplicate]

This is a small challenge which was given in my workplace, ...
0
votes
2answers
159 views

Map an array of functions to their return values in point-free style

Introduction I have some JavaScript code that uses Array.prototype.map to map an array of functions fns to their return values: ...
16
votes
6answers
2k views

Eating fish, growing in size

You are fish in a pond that needs to survive by eating other fish. You can only eat fish that are the same size or smaller than yourself. You must create a program that takes a shoal of fish as sorted ...
1
vote
0answers
93 views

Rewrite the Tiny BASIC IL program to support 7 changes to BASIC [closed]

Circa 1976. With the success of the Tiny BASIC Design Note, a variety of Tiny BASIC IL implementations now span a range of microcomputers. The time has come to expand the Tiny BASIC standard. Grammar ...
13
votes
5answers
1k views

Add comments to a Python script and make it a bilingual Python/C++ “program”

Given the following Python 3 script: def greet(): print("Hello, world!") greet() Prepend some lines to this text file so that it can be both executed as a ...
11
votes
3answers
922 views

C# is the Main problem

This programming puzzle is inspired by another question which has been asked here yesterday but which was deleted by the author... The challenge: Create an executable binary (Windows .EXE or Linux ...
1
vote
1answer
162 views

Hiding elements with CSS only [closed]

In the following markup without changing it or using JS, implement the behavior to show only the ul element of div which name is ...
19
votes
1answer
1k views

A little bool magic

Challenge Given the following C# method: ...
25
votes
11answers
2k views

Official Dyalog APL 2016 Year Game

If you think this could be fun, but too much work, consider participating in this much smaller challenge. A bit of fun (and possibly frustration!) for 2016... Dyalog's "puzzle of the year". Enjoy! ...
15
votes
2answers
620 views

Befunge Brain Teasers

Introduction For the ones wondering what Befunge exactly is, it is a two-dimensional stack based language made in 1993 by Chris Pressy. I made 7 brain teasers that need to be solved in Befunge-93. ...
7
votes
1answer
496 views

The Swift Make-It-Compile Challenge #4 [closed]

This is a challenge for the people using Swift 2.0 beta 6, it's not code golf, but it's a programming puzzle so I'm pretty sure this belongs here. The basic idea is: Make it compile. There are more ...
4
votes
0answers
616 views

C# program containing `? null :` that is still valid without `null` [closed]

I found an old post on Jon Skeet's blog that pointed out a puzzle from a tweet by Vladimir Reshetnikov: C# quiz: write a valid C# program containing a sequence of three tokens ...
10
votes
7answers
2k views

Non-idempotent Python [closed]

Write a few lines of Python code, X, that does not reference any global variables, such that ...
7
votes
0answers
792 views

Tricky interview puzzle: get longest sequence by flipping 1 bit [closed]

A friend of mine got this question during an interview. The interview ended with no luck for him, but still, we're very curious to hear the solution. The question is as follows: Modify the code below ...
-1
votes
2answers
453 views

Make the function comparing two variables with different value print True [closed]

Without changing the actual function, call it in such a way that it prints "True" ...
2
votes
0answers
153 views

Minimum number of coins to get -closest- to target value? [closed]

There's the old, well hashed version of the question that uses Dynamic Programming to calculate the minimum number of coins to reach a target value, but what if the coins you're given can't reach the ...
30
votes
22answers
3k views

Create an object whose state changes on assignment

I find it deeply weird that this is possible in Ruby (I won't immediately say how): ...
16
votes
5answers
1k views

reach catch when everything in the try block is caught already

This is limited to Java and C# by the syntax I guess. In this programming puzzle, you are to produce Exceptions that can be caught but are thrown again at the end ...
101
votes
157answers
15k views

Execute prints backwards

Your task is to reverse the order in which some prints get executed. Specs: Your code will be in this form: ...
3
votes
1answer
2k views

Match strings whose length is isolated prime [closed]

Isolated prime definition from Wikipedia: An isolated prime is a prime number p such that neither p − 2 nor p + 2 is prime. In other words, p is not part of a twin prime pair. For example, 23 is an ...
23
votes
4answers
4k views

Match strings whose length is a fourth power

Within the scope of this question, let us consider only strings which consist of the character x repeated arbitrary number of times. For example: ...
2
votes
10answers
4k views

Can you cancel the stackoverflow exception in the following code? [closed]

The following code will produce a run-time error, stackoverflow exception. ...
15
votes
6answers
4k views

Code Injection works in C# too!

Given the following C# program outputting False, inject a 'malicious' line of code such that the program outputs True. ...
4
votes
1answer
308 views

Decompose a range in aligned blocks of size 2^n [closed]

Given an arbitrary contiguous range of positive integers, find the decomposition in the minimum number of sub-ranges of size L = 2^n, with the constraint that each range must be aligned, that is the ...
14
votes
4answers
1k views

No branching please

Anyone who is moderately into low level code optimization know about the perils of branching, be it implemented as if-statements, loops or select-statements the possibility of a branch misprediction ...
22
votes
1answer
2k views

When is a giraffe not a giraffe?

I hope this kind of "riddle" is ontopic in Programming Puzzles & Code Golf. Give an example of a situation where the C# method below returns false: ...
2
votes
1answer
754 views

Write the implementation to pass this test

Given the following test, implement an addOne function in C# so it passes, without any modification to the test. TIP: Yes, it is possible. ...
11
votes
5answers
2k views

m3ph1st0s's programming puzzle 3 (C): “Easy bug” [closed]

This is the 3rd of my series of C/C++ puzzles; in case you missed the first 2 they are here: (1) m3ph1st0s's programming puzzle 1 (C++) (2) m3ph1st0s's programming puzzle 2 (C++): "Call ...
5
votes
4answers
772 views

m3ph1st0s's programming puzzle 2 (C++): “Call hard!” [closed]

I am back with a brand new puzzle for C/C++ addicts. Following the feedback received on the 1st puzzle, this time I will try to make the requirements crystal clear and to provide a perfectly valid ...
26
votes
17answers
3k views

m3ph1st0s's programming puzzle 1 (C++) [closed]

This is the first of a series of C++ puzzles for you. Hope you will enjoy. So, puzzle no.1: Given the following program: ...
3
votes
2answers
707 views

Generate N number of “Go First” dice [closed]

Background As described here http://www.ericharshbarger.org/dice/#gofirst_4d12, "Go First" Dice is a set of four dice, each with unique numbering, so that: There will never be a tie. Each die has a ...
10
votes
2answers
656 views

Which Numbers Would Crash this Function?

Which values of x and y will cause a crash with some C compilers? int f(int x, int y) { return (y==0) ? 0 : (x/y); }
4
votes
13answers
6k views

Count the number of times a function runs [closed]

This is a teaser a friend of mine sent. It seems straightforward, but I can't seem to get my head around it, probably due to my lack of sleep. You have 16 bits available. Compose a function, and ...
6
votes
3answers
568 views

Break the broken hash [closed]

Some time ago, I found this unused hash function (written in Java) in our codebase: ...