Browsing the blog archives for January, 2010

Trailing Zeros in 100 Factorial

Question: How many zeros are there in 100! (100 factorial)? Answer: For those who don’t know what factorial is, 100! = 100 * 99 * 98 * … * 2 * 1 Ok, let’s look at how trailing zeros are formed in the first place. A trailing zero is formed when a multiple of 5 is multiplied […]

5 Comments

Loop in a Singly-Linked List

Question: How would you find a loop in a singly-linked list? Answer: This is a very typical tech interview question that has a very elegant solution that runs in O(n) time and O(1) space. Also referred to as the “Tortoise and Hare Algorithm”,  the solution uses a fast pointer (traversing the list two items at […]

100 Prisoners in Solitary Cells

Question: 100 prisoners are stuck in the prison in solitary cells. The warden of the prison got bored one day and offered them a challenge. He will put one prisoner per day, selected at random (a prisoner can be selected more than once), into a special room with a light bulb and a switch which controls the […]

4 Quarts of Water

Question: If you had an infinite supply of water and a 5 quart and 3 quart pails, how would you measure exactly 4 quarts? and What is the least number of steps you need? Update: There is a better solution in the comments section which requires only 6 steps. Answer: This question is very simple […]

12 Comments

12 Identical Balls Problem

Question: You are give 12 identical looking balls. One of them is fake (could be heavier or lighter) than the rest of the 11 (all the others weight exactly the same). You a provided with a simple mechanical balance and you are restricted to only 3 uses. Find the fake ball. Answer: For convenience sake, […]

12 Comments

A Box of Defective Balls

Question: You have 10 boxes of balls (each ball weighing exactly10 gm) with one box with defective balls (each one of the defective balls weigh 9 gm). You are given an electronic weighing machine and only one chance at it. How will find out which box has the defective balls? Answer: For convenience sake, let’s […]

13 Comments

8 Identical Balls Problem

Question: You are given 8 identical looking balls. One of them is heavier than the rest of the 7 (all the others weigh exactly the same). You a provided with a simple mechanical balance and you are restricted to only 2 uses. Find the heavier ball. Answer: For convenience sake, let’s name the balls 1-8. […]

6 Comments

Little Endian or Big Endian?

Question: Is your machine little or big endian? Answer: Let first take a look at the solution before we discuss about it. int isLittleEndian( void ) { unsigned int temp = 0x12345678; char * tempAddress = &temp; if( *tempAddress == 0x12 ) { return 0; // Indicating False } else { return 1; // Indicating […]

3 Comments

Gold for 7 Days of Work

Question: You’ve got someone working for you for seven days and a gold bar to pay them. You must pay the worker for their work at the end of every day. If you are only allowed to make two breaks in the gold bar, how do you pay your worker? (Assuming equal amount of work […]

Reverse the Order of Words in a String

Question: Reverse the order of words in a string in C using as little additional memory as possible. Answer: The solution for this questions uses the solution of Reverse a String. I am going to make a small change to that solution by adding a length parameter. It will probably be better for you to […]