Questions tagged [puzzle]

DO NOT USE - prefer constructive questions, or use more descriptive tags. Otherwise, your question might be appropriate for codegolf.stackexchange.com.

DO NOT USE - prefer constructive questions, or use more descriptive tags. Otherwise, your question might be appropriate for CodeGolf.


The questions tagged "puzzle" are essentially programming questions, that are as good as solving puzzles. These can be contrasted to the real-life problems, which are comparatively easy to solve.

859 questions
42
votes
16 answers

Overload a C++ function according to the return value

We all know that you can overload a function according to the parameters: int mul(int i, int j) { return i*j; } std::string mul(char c, int n) { return std::string(n, c); } Can you overload a function according to the return value? Define a…
Motti
  • 110,860
  • 49
  • 189
  • 262
40
votes
26 answers

Fastest algorithm for circle shift N sized array for M position

What is the fastest algorithm for circle shifting array for M positions? For example, [3 4 5 2 3 1 4] shift M = 2 positions should be [1 4 3 4 5 2 3]. Thanks a lot.
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
40
votes
10 answers

Two marbles and a 100 story building

One of those classic programming interview questions... You are given two marbles, and told that they will break when dropped from some certain height (and presumably suffer no damage if dropped from below that height). You’re then taken to a 100…
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
40
votes
11 answers

Finding a single number in a list

What would be the best algorithm for finding a number that occurs only once in a list which has all other numbers occurring exactly twice. So, in the list of integers (lets take it as an array) each integer repeats exactly twice, except one. To find…
Vaibhav
  • 11,310
  • 11
  • 51
  • 70
32
votes
5 answers

Interesting OOPS puzzle

Recently, I faced the below question in an interview. Initially I thought that the question was wrong, but the interviewer mentioned there is a solution for this. Given this class: public class BaseHome { public static void Main() { …
user3714387
  • 353
  • 3
  • 3
28
votes
6 answers

Why does (x += x += 1) evaluate differently in C and Javascript?

If the value of the variable x is initially 0, the expression x += x += 1 will evaluate to 2 in C, and to 1 in Javascript. The semantics for C seems obvious to me: x += x += 1 is interpreted as x += (x += 1) which is, in turn, equivalent to x +=…
KT.
  • 10,815
  • 4
  • 47
  • 71
27
votes
10 answers

Solving Nonograms (Picross)

it's Friday afternoon, let's have a fun puzzle/algorithm problem to solve. One of my favorite Nintendo DS games is Picross DS. The game is quite simple, it involves solving puzzles called Nonograms. You can try a simple online Picross clone here:…
Chad Birch
  • 73,098
  • 23
  • 151
  • 149
27
votes
8 answers

Fixing a broken loop by changing exactly one character

I found a site with some complicated C puzzles. Right now I'm dealing with this: The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include int main() { …
Javier
  • 4,552
  • 7
  • 36
  • 46
27
votes
21 answers

Tape-Equilibrium Codility Training

I received a codility test the other day for a job, as such I've been practicing using some of the problems from their training page Link Unfortunately, I've only been able to get 83/100 on the Tape-Equilibrium question: A non-empty zero-indexed…
CTB
  • 283
  • 1
  • 3
  • 4
27
votes
12 answers

Multithreading Puzzles

I'm trying to come up with some programming puzzles focused on multi-threading. Most of the problems I've been able to come up with, so far, have been pretty domain specific. Does anybody have any decent programming puzzles for developers attempting…
senfo
  • 28,488
  • 15
  • 76
  • 106
26
votes
5 answers

Generalizing the algorithm for domino tiling?

In this earlier question the OP asked the following problem: Given a rectangular grid where some squares are empty and some are filled, what is the largest number of 2x1 dominoes that can be placed into the world such that no two dominos overlap…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
26
votes
3 answers

Simple Java Map puzzle

What is the best implementation for this general-purpose library method? public static boolean containsEntry( Map map, K key, V value) {} Criteria for judging this puzzle, as with most coding puzzles, are in this…
Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
25
votes
7 answers

C puzzle: Make a fair coin from a biased coin

How can I determine the probability that a function would return 0 or 1 in the following case: Let the function_A return 0 with probability 40% and 1 with probability 60%. Generate a function_B with probabilities 50-50 using only function_A …
garima
  • 5,154
  • 11
  • 46
  • 77
24
votes
4 answers

Question from Interview, Retrieve alphabetic order from dictionary

My girlfriend got this question in an interview, and I liked it so much I thought I'd share it... Write an algorithm that receives a dictionary (Array of words). The array is sorted lexicographically, but the abc order can be anything. For example,…
eladidan
  • 2,634
  • 2
  • 26
  • 39
23
votes
27 answers

Eric Lippert's challenge "comma-quibbling", best answer?

I wanted to bring this challenge to the attention of the stackoverflow community. The original problem and answers are here. BTW, if you did not follow it before, you should try to read Eric's blog, it is pure wisdom. Summary: Write a function that…
MMind
  • 1,845
  • 2
  • 17
  • 26
1
2
3
57 58