Puzzlers Main Page


Programming

Reverse a Linearly Linked List

Given a linear linked list, write a function that will reverse the linked list in O(n) time using a constant amount of extra memory.

Answer
Back to the Table Of Questions


Implment A Stack Using A Linked List

Given a linear linked list, write a function that will reverse the linked list in O(n) time using a constant amount of extra memory.

Answer
Back to the Table Of Questions


 Detecting Palindromes

There are three variants of this question, but the basic idea is essentially the same. A palindrome is a string of values such that the first value is the same as the last value, the second value is the same as the second to last value, etc. Thus, 101, 111, 11, AAAA, and 1122330332211 are palindromes.

Answer
Back to the Table Of Questions


Determine the Binary Representation of a given number

Given a long integer, write a function which will display which bits are 1. In other words, if you call the function with the argument 4, it should say something like:
(MSB)

00000000
00000000
00000000
00000100

(LSB)

Answer
Back to the Table Of Questions


Write QuickSort

Back to the Table Of Questions


Write BASIC's Left and Right functions

char *Left( char *sz, int n); // returns the first n characters of the string sz
char *Right(char *sz, int n); // returns the last n characters of the string sz
//more precisely, it will return a string that starts at the nth character of sz

NEED AN ANSWER
Back to the Table Of Questions


Swap values w/o using more memory

Write code in C to swap the values of two integers i and j without allocating any additional memory. What happens if i & j are the same variable (e.g., they're both variables passed by reference?)

Answer
Back to the Table Of Questions


Eliminate duplicate values in an array

Given an array of n sorted elements, write a function Compact that will remove all duplicate elements. Compact should be able to compact the array in-place, or copy the (non duplicate) values to somewhere else. You may assume that the array is of integers.

NEED AN ANSWER
Back to the Table Of Questions


Numbers in base -2

Consider the base -2 number system. That is, the places are worth:
-2^0 = 1
-2^1 = -2
-2^2 = 4
-2^3 = -8
-2^4 = 16
Etc.
Figure out an algorithm for adding two such numbers. Converting to binary? Converting from binary? Finding the complement?

NEED AN ANSWER
Back to the Table Of Questions


Write an algorithm for x^y

Write an efficient algorithm for finding x ^y (x raised to the power of y), where x is an int and y is a positive integer.
int PowerOf( int x, unsigned int y)

NEED AN ANSWER
Back to the Table Of Questions


Write pair of functions that deal cards

Write a pair of functions to deal cards. One functions initializes the deck, the other function returns a card. A card is a number that ranges from 0 to 51. The algorithm should be efficient.

NEED AN ANSWER
Back to the Table Of Questions


Mystery Function Number One

unsigned int foo( unsigned int i)
{
     unsigned int j = 0;
     while( i )
     {
         ++j;
         i &= i - 1;
     }
return j;
}

Answer
Back to the Table Of Questions


Mystery Function Number Two

What is the output of these lines of C code:

        for (i = 0; i <2; i++) for (j="2;" j> 0; j--) {
                        if (i == j)
                                continue;
                        printf("Out");
                        }


NEED AN ANSWER
Back to the Table Of Questions


General Questions

Gold Mine

You own a gold mine, and you have only one worker. This worker is paid 1 ounce of gold per day. Being a hard worker, the worker works seven days a week. To pay this worker, you have a single, seven ounce bar of gold. How can the bar be cut ONLY twice so that the worker gets exactly his total pay on each day? (I.E., at the end of the first day, the worker possesses one ounce of gold. At the end of the second day, the worker possesses a total of two ounces of gold, three ounces are possessed at the end of the third day, etc)

Answer
Back to the Table Of Questions


Light Bulb

Given: There exist two rooms, and in one room there are three light switches, all start in the " down " position. In the other room, there exist three light bulbs, all of which start in the " off " state. You are allowed to enter each room once AND only once in any order you please, but once you've left the room, you may never reenter or perceive the contents. You cannot perceive the contents of either room from outside the room. How can you determine which light switch controls which incandescent light bulb?

Answer
Back to the Table Of Questions


The Golf Ball Question

Given: A red colored basket containing an effectively infinite number of red colored (and ONLY red colored) golf balls and a green colored basket containing an effectively infinite number of green colored (and ONLY green colored) golf balls. A >B>Cycle consists of taking G (some arbitrary number) of golf balls out of one basket and dropping them into the basket of the other color, and then taking G (same number as before) randomly selected golf balls out of the second basket, and dumping that handful into the first basket. It doesn't matter which color basket you start with, and you can pick up golf balls that you just dropped into a basket (so you could pick 5 red golf balls out of the red basket, dump them into the green basket, and then pick 5 red golf balls out of the green basket and put them into the red>
Prove that the following statement is true: After an arbitrary number of cycles, the number of red golf balls in the green basket is equal to the number of green golf balls in the red basket.
There is a similar question about two colors of paint, except that the question is phrased "You have two colors of paint. You take some paint out of one can and dump it into the other can. You take an identical volume of paint of that can and dump it back into the first can. After doing this for (N) steps, how much oppositely colored paint does each can contain?"
NEED AN ANSWER
Back to the Table Of Questions


How to get four gallons using 5 and 3 gallon bottles

For anyone whoÕs ever seen Die Hard 3: Die Hard with a Vengeance (A.k.a., Die Hard 3: Die Hardest), this should be familiar. The situation is this: You are standing in a pool with a five gallon bottle and a three gallon bottle, and you have to figure out how to get PRECISELY four gallons into the five gallon bottle. ThereÕs an effectively unlimited supply of water that youÕre standing in, but you canÕt use any other containers, measuring devices, or modify either of the bottles (such as by marking them)

Answer
Back to the Table Of Questions


Mixing Paint

A cup of blue pigment and a cup of red pigment are taken from their respective cans and dumped into the same empty can. The pigments are then mixed vigorously. One cup of pigment from the mixed can is then taken and dumped into the blue can. The remainder of the mixed can is the poured into the red can. How much red is in the blue can, and how much blue is in the red can?

NEED AN ANSWER
Back to the Table Of Questions


Walking Around....

How many starting points are there on the Earth such that if a person traveled one mile south, one mile east and on mile north (s)he would end up in the placed that (s)he started at?

NEED AN ANSWER
Back to the Table Of Questions


Given ten masses and a scale...

Given ten masses, nine of which are equivalent, and one balance, how can you use the balance no more than four times and discover which masses is not the equivalent one?(Note: The solution should work if the unique mass is less than or greater than the nine equal masses)

NEED AN ANSWER
Back to the Table Of Questions


Given:A Scale and Ten Boxes

Given: 10 "Big" Boxes, each of a unique color (for identification), each containing 10 "Small" boxes (of the same color as the big box they're in). Each of the small boxes contains 10 ball bearings in 9 of the ten big boxes. One of the big boxes had a problem during manufacturing and each of it's small boxes now only contains nine Ball Bearings each (one short). You can open the big boxes and remove the small boxes, but you cannot open the small boxes. The scale has only one platform (it's not a balancing scale, but a bathroom-type scale), and will read in units equal to 1 Ball Bearings weight (the boxes themselves are of negligible weight). For example, if you put a small box containing 10 Ball Bearings, it would read 10.
If you can use the scale once, how can you find out which big box is incorrect (contains only 9 Ball Bearings per Small box)?

NEED AN ANSWER
Back to the Table Of Questions


Ice cube melting

You are given a glass of pure water with an ice cube in the glass. The water in the glass comes up to the rim of the glass exactly.When the ice cube melts, will the water overflow?

Answer
Back to the Table Of Questions


Sum the integers from 1 to 100 in your head

Quickly sum up all the integers between 1 and 100 (inclusive at both ends) quickly, in your head

NEED AN ANSWER
Back to the Table Of Questions


Infinite red-black plane

You have an infinite x,y plane. Each point is colored arbitrarily either red or black. Proove that given any distance, you can find two points on the plane that are the same color & that are that distance apart. (NB: A formal proof is not needed. )

NEED AN ANSWER
Back to the Table Of Questions


Joe and Fred vs. The Train

Joe and Fred are standing in a underground tunnel for trains, and they are W meters away from the nearest end. A train enters the tunnel's nearest end traveling at X meters per second. At the instant that the train enters the tunnel, both Joe and Fred begin running for an exit: Joe for the far exit, and Fred from the near end (ie, towards the oncoming train). If Joe and Fred both move at Y meters per second, how far will Joe have gone when Fred gets hit by the oncoming train?

NEED AN ANSWER
Back to the Table Of Questions