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 ...

learn more… | top users | synonyms

17
votes
1answer
327 views

A little bool magic

Challenge Given the following C# method: private static bool Test(bool a, bool b) { if (a && b) return false; if (a) if (b) return true; return false; } Supply the values a and ...
21
votes
8answers
696 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
320 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
249 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
360 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 ? null : that ...
10
votes
7answers
1k views

Non-idempotent Python [closed]

Write a few lines of Python code, X, that does not reference any global variables, such that def method(): X print(a) method() prints 1 but def method(): X X print(a) ...
5
votes
0answers
363 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
335 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" void Puzzle(out int x, out int y) { x = 0; y = 1; Console.WriteLine(x == y); } Online Tester
2
votes
0answers
112 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): obj = #code redacted print obj.state # Some value. LValue = obj print obj.state # Different value! Your ...
14
votes
6answers
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 of the catch block. try { ...
90
votes
148answers
12k 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: //some lines of code /*code*/ print "Line1" /*code*/ /*code*/ print "Line2" /*code*/ ...
3
votes
1answer
2k views

Match strings whose length is isolated prime

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 ...
22
votes
4answers
2k 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: <empty> x xx xxxxxxxxxxxxxxxx (Well, ...
3
votes
10answers
3k views

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

The following code will produce a run-time error, stackoverflow exception. class Foo { //static Foo foo = new Foo(); // you are not allowed using this approach //static readonly Foo foo = ...
14
votes
5answers
2k 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. class Program { static void Main() { ...
4
votes
1answer
292 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 ...
11
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 ...
19
votes
1answer
1k 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: public class Giraffe : Animal { public bool ...
2
votes
1answer
651 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. [TestMethod] public void TheChallenge() { int a = 1; ...
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
652 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: #include <iostream> int main() { const int a=1; const int b=2; ...
3
votes
2answers
682 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 ...
10
votes
2answers
582 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); }
5
votes
13answers
2k views

Count the number of times a function runs

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
515 views

Break the broken hash [closed]

Some time ago, I found this unused hash function (written in Java) in our codebase: long hashPassword(String password) { password = password.toUpperCase(); long hash = 0; int multiplier = ...