Questions tagged [puzzle]

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

puzzle
Filter by
Sorted by
Tagged with
718 votes
79 answers
225k views

Expand a random range from 1–5 to 1–7

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.
386 votes
35 answers
233k views

How to find list of possible words from a letter matrix [Boggle Solver]

Lately I have been playing a game on my iPhone called Scramble. Some of you may know this game as Boggle. Essentially, when the game starts you get a matrix of letters like so: F X I E A M L O E W B X ...
206 votes
46 answers
494k views

What is the simplest SQL Query to find the second largest value?

What is the simplest SQL query to find the second largest integer value in a specific column? There are maybe duplicate values in the column.
Niyaz's user avatar
  • 54.4k
153 votes
31 answers
61k views

Set every cell in matrix to 0 if that row or column contains a 0

Given a NxN matrix with 0s and 1s. Set every row that contains a 0 to all 0s and set every column that contains a 0 to all 0s. For example 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 results ...
jaircazarin-old-account's user avatar
117 votes
37 answers
225k views

How to find all combinations of coins when given some dollar value [closed]

I found a piece of code that I was writing for interview prep few months ago. According to the comment I had, it was trying to solve this problem: Given some dollar value in cents (e.g. 200 = 2 ...
100 votes
31 answers
56k views

Programmer Puzzle: Encoding a chess board state throughout a game

Not strictly a question, more of a puzzle... Over the years, I've been involved in a few technical interviews of new employees. Other than asking the standard "do you know X technology" questions, I'...
93 votes
16 answers
131k views

How to generate Sudoku boards with unique solutions

How do you generate a Sudoku board with a unique solution? What I thought was to initialize a random board and then remove some numbers. But my question is how do I maintain the uniqueness of a ...
guilin 桂林's user avatar
78 votes
8 answers
10k views

The "guess the number" game for arbitrary rational numbers?

I once got the following as an interview question: I'm thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) queries. Each query is a number of your choosing, ...
templatetypedef's user avatar
61 votes
7 answers
22k views

hello world in C without semicolons and without IF/WHILE/FOR statements [closed]

My friend says it's possible to write a C program that will print "hello world" without IF/WHILE/FOR and without semicolons. After minimal research I told her it was not possible. Is it ...
Alex Gordon's user avatar
  • 59.4k
56 votes
14 answers
51k views

Algorithm to generate anagrams

What would be the best strategy to generate anagrams. An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the ...
prakash's user avatar
  • 59.4k
48 votes
7 answers
65k views

java based programming challenges [closed]

Is there any website which provides core java based programming challenges that tests programming logic?
abson's user avatar
  • 9,268
47 votes
11 answers
16k views

How to test randomness (case in point - Shuffling)

First off, this question is ripped out from this question. I did it because I think this part is bigger than a sub-part of a longer question. If it offends, please pardon me. Assume that you have a ...
Tnilsson's user avatar
  • 2,180
47 votes
39 answers
31k views

Algorithm to determine if array contains n...n+m?

I saw this question on Reddit, and there were no positive solutions presented, and I thought it would be a perfect question to ask here. This was in a thread about interview questions: Write a method ...
45 votes
28 answers
25k views

Programming Riddle: How might you translate an Excel column name to a number?

I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It's about translating Excel column letters to actual numbers, if you recall, ...
42 votes
16 answers
27k views

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 ...
Motti's user avatar
  • 113k
42 votes
5 answers
14k views

Linear Time Voting Algorithm. I don't get it

As I was reading this (Find the most common entry in an array), the Boyer and Moore's Linear Time Voting Algorithm was suggested. If you follow the link to the site, there is a step by step ...
Lieven Keersmaekers's user avatar
40 votes
11 answers
24k views

Finding a single number in a list [duplicate]

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 ...
Vaibhav's user avatar
  • 11.4k
40 votes
26 answers
39k views

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's user avatar
40 votes
10 answers
22k views

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 ...
Matt Sheppard's user avatar
32 votes
5 answers
3k views

Interesting OOPS puzzle [closed]

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 ...
user3714387's user avatar
28 votes
6 answers
3k views

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 += ...
KT.'s user avatar
  • 11.1k
28 votes
10 answers
41k views

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. ...
Chad Birch's user avatar
  • 73.8k
27 votes
21 answers
61k views

Tape-Equilibrium Codility Training [closed]

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-...
CTB's user avatar
  • 283
27 votes
8 answers
4k views

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 ...
Javier's user avatar
  • 4,563
27 votes
12 answers
6k views

Multithreading Puzzles [closed]

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 ...
senfo's user avatar
  • 28.8k
26 votes
7 answers
13k views

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 ...
garima's user avatar
  • 5,194
26 votes
5 answers
9k views

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 ...
templatetypedef's user avatar
26 votes
3 answers
3k views

Simple Java Map puzzle [closed]

What is the best implementation for this general-purpose library method? public static <K, V> boolean containsEntry( Map<K, V> map, K key, V value) {} Criteria for judging this ...
Kevin Bourrillion's user avatar
24 votes
4 answers
14k views

Question from Interview, Retrieve alphabetic order from dictionary [closed]

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, ...
eladidan's user avatar
  • 2,642
23 votes
27 answers
5k views

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, ...
MMind's user avatar
  • 1,885
23 votes
7 answers
16k views

Single Number II from leetcode

The question about Single Number II from leetcode is: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear ...
CSnerd's user avatar
  • 2,159
22 votes
5 answers
27k views

SQL: how to get all the distinct characters in a column, across all rows

Is there an elegant way in SQL Server to find all the distinct characters in a single varchar(50) column, across all rows? Bonus points if it can be done without cursors :) For example, say my data ...
frankadelic's user avatar
  • 20.7k
21 votes
4 answers
879 views

C Code: How does these even work?

I just saw this here #include <stdio.h> int main(int argc, char *argv[printf("Hello, world!\n")]) {} What this does is print "Hello World!" But what's actually going on here? The best I can ...
Reverend Gonzo's user avatar
21 votes
3 answers
5k views

Construct the largest possible rectangle out of line segments of given lengths

I recently participated in a competition where I was asked this question. Given an array with lengths what is the area of the biggest rectangle that can be made using ALL the lengths. The lengths can ...
johne's user avatar
  • 401
20 votes
7 answers
5k views

cat file | ... vs ... <file

Is there a case of ... or context where cat file | ... behaves differently than ... <file?
namin's user avatar
  • 37.6k
19 votes
15 answers
36k views

C# Potential Interview Question…Too hard? [closed]

Without running this code, identify which Foo method will be called: class A { public void Foo( int n ) { Console.WriteLine( "A::Foo" ); } } class B : A { /* note that A::Foo and B:...
19 votes
10 answers
60k views

Check if a number is divisible by 3 [closed]

Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a ...
Eyal's user avatar
  • 5,768
19 votes
6 answers
36k views

What can be the efficient approach to solve the 8 puzzle problem?

The 8-puzzle is a square board with 9 positions, filled by 8 numbered tiles and one gap. At any point, a tile adjacent to the gap can be moved into the gap, creating a new gap position. In other words ...
Ravindra S's user avatar
  • 6,352
19 votes
9 answers
18k views

Solving Puzzle in Python

I got one puzzle and I want to solve it using Python. Puzzle: A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 4 pieces. But ...
Abid Rahman K's user avatar
19 votes
14 answers
2k views

Code Golf: Automata

I made the ultimate laugh generator using these rules. Can you implement it in your favorite language in a clever manner? Rules: On every iteration, the following transformations occur. H -> ...
19 votes
2 answers
5k views

Programming two trains to intersect without positional data or communication (logic puzzle) [closed]

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is an undefined distance between the two trains. Each faces the same direction, and upon landing, the ...
siva's user avatar
  • 1,703
19 votes
6 answers
1k views

Print number of 1s in a sequence up to a number, without actually counting 1s [closed]

An interview question: Make a program which takes input 'N'(unsigned long) and prints two columns, 1st column prints numbers from 1 to N (in hexadecimal format) and second column prints the number of ...
Manish Baphna's user avatar
19 votes
4 answers
2k views

What is your solution to the "Escape from Zurg" puzzle in C#?

found this puzzle HERE... I made a brute force solution and I would like to know how you woul solve it... Buzz, Woody, Rex, and Hamm have to escape from Zurg(a) They merely have to cross one last ...
18 votes
37 answers
5k views

Programming Riddle: Counting down without subtracting [closed]

Ok, goal by example : a command-line app that does this: Countdown.exe 7 prints 7 6 5 4 3 2 1 No form of subtracting (including use of the minus sign) or string reverse what so ever is allowed. ...
18 votes
16 answers
3k views

C puzzle: Output of printf should be '5' always

I found this puzzle in a C aptitude paper. void change() { //write something in this function so that output of printf in main function //should always give 5.you can't change the main ...
SyncMaster's user avatar
  • 9,818
18 votes
8 answers
11k views

i = ++i + ++i; in C++ [duplicate]

Can someone explain to me why this code prints 14? I was just asked by another student and couldn't figure it out. int i = 5; i = ++i + ++i; cout<<i;
Alexander Stolz's user avatar
18 votes
12 answers
15k views

How do I programmatically return the max of two integers without using any comparison operators and without using if, else, etc?

How do I programmatically return the maximum of two integers without using any comparison operators and without using if, else, etc?
MrDatabase's user avatar
18 votes
4 answers
11k views

Designing a twenty questions algorithm

I am interested in writing a twenty questions algorithm similar to what akinator and, to a lesser extent, 20q.net uses. The latter seems to focus more on objects, explicitly telling you not to think ...
IVlad's user avatar
  • 43.3k
17 votes
12 answers
32k views

Bridge crossing puzzle

Four men have to cross a bridge at night.Any party who crosses, either one or two men, must carry the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, etc. ...
Ganesh M's user avatar
  • 3,696
17 votes
9 answers
9k views

What is an elegant way to check if 3 variables are equal when any of them can be a wildcard?

Say I have 3 char variables, a, b and c. Each one can be '0', which is a special case and means it matches every char. So if a is '0', I only need to check if b == c. I want to check if a == b == c, ...
colinfang's user avatar
  • 21.4k

1
2 3 4 5
17