A challenge to write a program that looks like it does one thing, but, in reality, does something else. Votes reflect how non-obvious the bug is.

learn more… | top users | synonyms

6
votes
1answer
289 views

You call this optimized? [closed]

The task is simple, post 2 code snippets that perform the same task, where one seems very optimized at first glance, but actually is significantly slower than the other. Only one rule: Both ...
-6
votes
1answer
347 views

Evil use of C/C++ macros [closed]

Write a program that uses macros to produce an unexpected result in a sorting program. Bonus for not being obvious. For example, #define one 2 would be obvious. #define FALSE 1==2 would be ...
-1
votes
4answers
412 views

Like a quine, but not

Very simple, write a program that at first glance appears to be a quine, but is not. Upon running, the program actually prints the source to a real quine, obviously distinct from the original program ...
14
votes
6answers
801 views

Classic Proof Fallacy

The Background So, we all know the classic proof that goes like this: a = b a² = ab a² - b² = ab - b² (a-b)(a+b) = b(a-b) (a+b) = b b+b = b 2b = b 2 = 1 (Ha ha!) Of course, the mistake is that ...
49
votes
2answers
3k views

Quiche Lorraine

Since it was Pi day recently, I have noticed a number of challenges that ask you to calculate pi. Of course, a quiche lorraine is not quite a pie (you can claim a Bonus Score¹ of +1 if you guessed ...
7
votes
3answers
893 views

Recognize Twin Primes by Crashing

Background Story Bob was handed the following assignment for his programming class. Please note: this is not the actual challenge. Homework assignment 11: Twin primes Input: an integer n ...
-5
votes
1answer
246 views

Number guessing game with memory [closed]

Your programme should ask the user's name and then play a number guessing game: (>) means that the computer writes it. > What's your name? Brown > Brown you are welcome. What is your number? ...
19
votes
6answers
2k views

Confuse the Dakotas

You have been hired by the government of North Dakota to encrypt the state's communications. Write two programs, one to encrypt a message and one to decrypt that message. The government is not really ...
4
votes
1answer
1k views

Break Stack Exchange

Trick the user that StackExchange is broken: It can be done in several ways: Your program starts a browser in some certain way (with a proxy server, for example); Your program shows static ...
29
votes
9answers
1k views

Sabotage the coding standards

There are various coding standards enforced at software companies which have the goal of increasing code reliability, portability, and, most importantly, readability in code written jointly by ...
14
votes
1answer
688 views

Sabotage a Train to Make It Run Late

"I want to go to the Araby bazaar to buy a present for the one I have fallen in love with. However, if I arrive too late all the stores will be closed and I won't be able to buy anything. Can you help ...
12
votes
2answers
824 views

Underhanded Bank Account

You are working as a programmer for a bank. Your task is to write a program that handles the transactions from one bank account to another. The program should do the following: Run in an infinite ...
74
votes
7answers
7k views

Start a revolution in Oceania

Winners (decided 14/09/14) Winning answer by Markuz (Python) - 63 votes Runner up by kuroi neko (PHP) - 55 votes Introduction You are in the world of the book Nineteen Eighty-Four by George ...
2
votes
2answers
598 views

Memory leak, without any fork bombs or looping of any kind [duplicate]

Your mission (if you choose to accept it) is to create a memory leak. In a vein similar to this question, you must write a program that will continuously allocate/consume memory until the system runs ...
83
votes
15answers
16k views

Possibly quit your job with a polyglot

Despite your protest, you have been put to work by your boss on a program that takes a single unsigned integer as input and prints the string "prime" if that integer is prime and "not prime" if it ...
1
vote
2answers
635 views

Print all multiples of 32 from 0 to `n`…imperfectly

Objective: Print all multiples of 32 from 0 to an arbitrary number n. Rules: One argument will be given: a single integer from 0 to 231 - 1. This will be n here on out. The first iteration must be ...
2
votes
7answers
653 views

Break out of a loop [duplicate]

Now that we have over a hundred ways to get stuck in a loop, the next challenge is to get out. Specifically, write a piece of code with a (seemingly) infinite loop, but somehow manage to break out. ...
1
vote
11answers
1k views

Implement `map()`…with bugs

EDIT: The behavior may be inconsistent. Also, associative arrays are permitted. Make a function that iterates through an indexed array, mapping a function of a single argument to each member of the ...
2
votes
8answers
844 views

Return Zero in the Least Intuitive Way [closed]

Write a function which returns 0 (the number, or string "0" or "zero"). At first glance it should look like your function returns a different value. Standard loopholes apply. Winner will be the ...
1
vote
1answer
397 views

Fake infinite loop [closed]

The challenge is to write a fake infinite loop in any imperative programming language. At first sight, the code should seem not to halt at all. As a naive example: int f(int x) { return f(4.0); ...
49
votes
14answers
8k views

“Disprove” Fermat's Last Theorem

Write a program, in the language of your choice, that appears to successfully find a counterexample to Fermat's Last Theorem. That is, find integers a, b, c > 0 and n > 2 such that an + bn = cn. Of ...
34
votes
8answers
3k views

It's not who votes that counts; it's who counts the votes

The scenario You live in a country that is having a presidential election. Each voter gets one vote, and therefore there is a firmly-entrenched two-party system. (Third parties exist, but get ...
102
votes
63answers
16k views

Random script that isn't actually random

As a little joke in the office someone wanted a script that randomly picks a name, and said person will make a round of drinks. Let's call the people John, Jeff, Emma, Steve and Julie. I thought it ...
34
votes
30answers
7k views

Deletion of a blank line in source code which causes unexpected functionality

Write a script that works fine when a blank line is present in the flow of program logic, but breaks or causes unexpected behaviour when that line is removed. Avoid standard loopholes and stupid ...
13
votes
0answers
881 views

Efficient error-free* encoding

The mission As is well known, the genetic material of all known creatures on Earth is encoded in DNA; using the four nucleotides adenine, thymine, cytosine, and guanine. (Commonly represented by ...
12
votes
6answers
2k views

Write a sorting program that looks erroneous but it is actually correct

Write a program that sorts a vector of numbers (or any type of element) that looks like having one or more bugs, but it is actually ok. The code must be clear. Someone looking over the code must ...
32
votes
42answers
5k views

Produce an unexpected word

The goal is to write program that will produce a word unexpectedly (program doesn't look like it is going to produce a word). Example (JavaScript): var ...
25
votes
8answers
8k views

Pi is still wrong

Pi is Wrong A common method of computing pi is by throwing "darts" into a 1x1 box and seeing which land in the unit circle compared to the total thrown: loop x = rand() y = rand() ...
321
votes
121answers
160k views

Write a program that makes 2 + 2 = 5

Write a program that seemingly adds the numbers 2 and 2 and outputs 5. This is an underhanded contest. Your program cannot output any errors. Watch out for memory holes! Input is optional. ...
49
votes
18answers
7k views

Program that adds all natural numbers and yields -1/12

As you might know there is a mathematical fun-fact that if you add all natural numbers you end up with... -1/12 (see Wikipedia here). Of course this is very strange result and can not be obtained by ...
11
votes
14answers
2k views

Throw a null-pointer exception

Your task is to generate a null-pointer exception. That is, your program must accept a value which it expects to be non-null, and throw an exception/error or crash because the value is null. ...
12
votes
1answer
745 views

Pathological Sorting

Pathological Sorting Your boss has demanded that you develop a sorting algorithm to improve the performance of your company's application. However, having written the application, you know that ...
11
votes
5answers
1k views

Cheap, Fast, Good - Common Factor (Greatest)

Inspired by Cheap, Fast, Good, we're going to implement an algorithm which has exactly two of them. The Math Given two nonzero integers a and b, the GCF d is the largest integer that divides both a ...
3
votes
1answer
893 views

Underhanded contest (?): Anti-Virus wars

Prove anti-virus software is paranoid by making a program that simply opens and alphabetically sorts a CSV file, that will get flagged as a virus (or malware/spyware/any threat really) when run. You ...
104
votes
7answers
21k views

So obviously, P = NP

SAT is the problem of determining whether a boolean expression can be made true. For example, (A) can be made true by setting A=TRUE, but (A && !A) can never be true. This problem is known to ...
92
votes
73answers
15k views

Why isn't it ending?

Your task: To write a program that should obviously terminate, but it never (to the extent of a computer crash) does. Make it look like it should perform a simple task: adding numbers, printing ...
22
votes
31answers
6k views

Obfuscation Challenge

You are to write a program that does one of the following. Only displays "Hello World" does nothing else Quits and nothing else (no output, no errors.) Takes one line of input, parses it as an ...
168
votes
45answers
28k views

Trolling the troll

A troll has captured you and is forcing you to write malware (defined as a program that damages the computer that runs it). The troll can read and understand code, but is not so good at spotting bugs. ...
11
votes
3answers
668 views

These are my lucky dice

Implement a program or function which simulates common dice for role playing games. It should handle at least the d6 and d20, the two most common dice. However, it should work as stereotypical gamers ...
188
votes
80answers
19k views

What? No error?

Your task is simple. Write a program that should obviously produce an error on first glance either when compiled or run, but either doesn't or produces some other unrelated error. This is a popularity ...
6
votes
8answers
606 views

Loophole for other challenge [closed]

You are to post an answer you posted that exploited a loophole in the challengers question. Bonus points if it caused them to rewrite the question. For example, one challenge was to write a program ...
28
votes
2answers
2k views

Underhanded code contest: Not-so-quick sort

The Task Write a program, in the language of your choice, that reads lines of input from standard input until EOF, and then writes them to standard output in ASCIIbetical order, similar to the sort ...
26
votes
17answers
4k views

Write a line in program that looks useless

Write some program (the size doesn't matter), and put a line that looks useless, but it's actually not - if it would be removed, the program would crash, or do something wrong. Feel free to mark the ...
7
votes
7answers
877 views

Find the largest prime under a number… but only for the test cases

The Code Your program should take an input of n > 2 and output the largest prime less than or equal to n. You can choose how the number is input (standard input, read from a file, etc). For example: ...
15
votes
1answer
3k views

Hide evil code - print files containing a given string anywhere in a directory tree

The point of this puzzle is to learn how a malicious code can be hidden and discovered in a program. A person is asking the question: Plz give me some code that how can I search a file may be in ...
17
votes
6answers
1k views

An unintentional-looking but crash-causing bug

There are a range of questions revolving around this concept, but these all seem to just involve causing a crash, resulting in many answers which are very obviously designed to cause a crash. So the ...
32
votes
80answers
7k views

Challenge: Write a piece of code that quits itself

I'm searching (am I?) for a piece of code that quits immediately - in an absolutely unconventional way. This does not mean: System.exit((int) 'A'); (Java). It might mean: #!/usr/bin/env python3 # ...
13
votes
3answers
1k views

Write a java code to detect the JVM version

The objective is to write java code that detects the JVM version relying in compatibility changes, side effects, bugs and/or undefined behavior that works in a way in one version and another way in ...
10
votes
2answers
854 views

Underhanded launcher

Your goal is to write a super-secret program which launches another (independent) program. You don't have to provide the payload in your answer, it can be any program name your program will launch. ...
28
votes
7answers
3k views

Underhanded contest: The OS war

We all know how the discussion about which is the best operating system caused a lot of flame-wars. Your goal is now, to provide decisive "proof" that your favorite operating system is better... ah, ...