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
18
votes
8 answers

i = ++i + ++i; in C++

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<
Alexander Stolz
  • 7,454
  • 12
  • 57
  • 64
18
votes
16 answers

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 function } int main() { int i = 5; change(); …
SyncMaster
  • 9,754
  • 34
  • 94
  • 137
17
votes
5 answers

Place random non-overlapping rectangles on a panel

I've a panel of size X by Y. I want to place up to N rectangles, sized randomly, upon this panel, but I don't want any of them to overlap. I need to know the X, Y positions for these rectangles. Algorithm, anyone? Edit: All the N rectangles are…
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
17
votes
9 answers

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, but found the implementation in C# goes chaotic…
colinfang
  • 20,909
  • 19
  • 90
  • 173
17
votes
6 answers

Why is my simple comparator broken?

I have a class, which I have simplified to this: final class Thing { private final int value; public Thing(int value) { this.value = value; } public int getValue() { return value; } @Override public String…
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
17
votes
5 answers

Modify a given number to find the required sum?

A friend of mine sent me this question. I haven't really been able to come up with any kind of algorithm to solve this problem. You are provided with a no. say 123456789 and two operators * and +. Now without changing the sequence of the provided…
Gaurav
  • 681
  • 3
  • 10
  • 21
17
votes
3 answers

The “pattern-filling with tiles” puzzle

I've encountered an interesting problem while programming a random level generator for a tile-based game. I've implemented a brute-force solver for it but it is exponentially slow and definitely unfit for my use case. I'm not necessarily looking for…
Trillian
  • 6,207
  • 1
  • 26
  • 36
17
votes
12 answers

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
  • 43,245
  • 41
  • 111
  • 153
17
votes
12 answers

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. Each man walks at a different speed. One takes 1…
Ganesh M
  • 3,666
  • 8
  • 27
  • 25
16
votes
6 answers

Pairwise sum of n numbers in non increasing order

I saw this question in a programming interview blog. If pairwise sums of n numbers are given in non-decreasing order identify the individual numbers. If the sum is corrupted print -1. Example: i/p: 4 5 7 10 12 13 o/p: 1 3 4 9 A hint would…
ash
  • 1,170
  • 1
  • 15
  • 24
16
votes
4 answers

function with the same name as a macro

#include void f(int a) { printf("%d", a); } #define f(a) {} int main() { /* call f : function */ } How to call f (the function)? Writing f(3) doesn't work because it is replaced by {}
Ferdy
  • 341
  • 3
  • 6
16
votes
10 answers

Compile time sizeof_array without using a macro

This is just something that has bothered me for the last couple of days, I don't think it's possible to solve but I've seen template magic before. Here goes: To get the number of elements in a standard C++ array I could use either a macro (1), or a…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
15
votes
21 answers

Efficiently reverse the order of the words (not characters) in an array of characters

Given an array of characters which forms a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Example input and output: >>> reverse_words("this is a string") 'string a is this' It should be O(N)…
jfs
  • 399,953
  • 195
  • 994
  • 1,670
15
votes
22 answers

Algorithm: Max Counters

I have the following problem: You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max_counter − all counters are set to the maximum value of any counter. A…
Randal Cunanan
  • 2,449
  • 6
  • 29
  • 38
14
votes
8 answers

Optimally picking one element from each list

I came across an old problem that you Mathematica/StackOverflow folks will probably like and that seems valuable to have on StackOverflow for posterity. Suppose you have a list of lists and you want to pick one element from each and put them in a…
dreeves
  • 26,430
  • 45
  • 154
  • 229