Question Bank

This page contains all the questions in this blog. It is updated once a week. This page is created just as an easy reference index page for all the questions and puzzles. Please feel free to link to this page.

Trains and Birds

A train leaves City X for City Y at 15 mph. At the very same time, a train leaves City Y for City X at 20 mph on the same track. At the same moment, a bird leaves the City X train station and flies towards the City Y train station at 25 mph. When the bird reaches the train from City Y, it immediately reverses direction. It then continues to fly at the same speed towards the train from City X, when it reverses its direction again, and so forth. The bird continues to do this until the trains collide. How far would the bird have traveled in the meantime?

Globe Walker

How many points are there on the globe where, by walking one mile south, then one mile east and then one mile north, you would reach the place where you started?

Clock Hands

How many times a day do the minute and hour hands of a clock overlap?

The Ant Problem

Three ants are sitting at the three corners of an equilateral triangle. Each ant starts randomly picks a direction and starts to move along the edge of the triangle. What is the probability that none of the ants collide?

Piece of Cake

How would you cut a rectangular cake into two equal pieces when a rectangular piece has already been cut out of it? The cut piece can be of any size and orientation. You are only allowed to make one straight cut.

Combinations in a String

Write an algorithm to print all possible combinations of characters in a string.

Red, Green and Yellow Balls

You are give two red balls, two green balls and two yellow balls. One ball from each color is heavier than the other one. All the heavier balls weigh the same and all the lighter balls weigh the same. You are only provided with a weighing balance. How many tries would it take to identify which one is heavier and which one is lighter?

Tunnel Trouble

A man needs to go through a train tunnel to reach the other side. He starts running through the tunnel in an effort to reach his destination as soon as possible. When he is 1/4th of the way through the tunnel, he hears the train whistle behind him. Assuming the tunnel is not big enough for him and the train, he has to get out of the tunnel in order to survive. We know that the following conditions are true.

1. If he runs back, he will make it out of the tunnel by a whisker.
2. If he continues running forward, he will still make it out through the other end by a whisker.

What is the speed of the train compared to that of the man?

First Non-Repeated Characters

Write an algorithm to find the first non-repeated character in a string. For example, the first non-repeated character in the string ‘abcdab’ is ‘c’.

Number of Ones

Write a function that determines the number of bits set to 1 in the binary representation of an integer.

Farmer’s Dilemma

A farmer bought a goat, a wolf and a cabbage from the market. On his way home, he has to cross a river. He has a small boat which only allows him to take one thing with him at a time. The farmer cannot leave the cabbage and the goat together (the goat would eat the cabbage) nor can he leave the goat and the wolf together (the wolf would eat the goat). How does he cross the river without losing any of the things he bought?

Anagrams in an Array of Strings

You are given an array of strings and you are asked to display all the anagrams within the array. For those who don’t know, two words are anagrams if they contain the same characters.

Permutations of a String

Print all the permutations of a string. Write code in C.

Nth-to-Last Element in a Singly Linked List

Devise an algorithm to determine the Nth-to-Last element in a singly linked list of unknown length. If N = 0, then your algorithm must return the last element.

Three Switches

You are standing outside a room next to three switches, all of which are off. Each switch operates a different light bulb in the room. The room door is closed, so you cannot see which switch operates which bulb. You are only allowed to go into the room once. Determine which switch operates which bulb.

Pick a Random Byte

You have a stream of bytes from which you can read one byte at a time. You only have enough space to store one byte. After processing those bytes, you have to return a random byte. Note: The probability of picking any one of those bytes should be equal.

C Keywords – Review

A quick look at some of the keywords in C.

9 Minutes

You are given two hourglasses. One measures 4 minutes and one measures 7 minutes. How would you measure exactly 9 minutes?

Singly Linked List – Delete Node

You are given a pointer to a node (not the tail node) in a singly linked list. Delete that node from the linked list. Write code in C.

Chess Squares

How many squares are on a chess board?

6 Pirates Fight for 1 Gold Coin

Six pirates discover a chest containing 1 gold coin. They decide to sit down and devise a distribution strategy. The pirates are ranked based on their experience (Pirate 1 to Pirate 6, where Pirate 6 is the most experienced). The most experienced pirate gets to propose a plan and then all the pirates vote on it. If at least half of the pirates agree on the plan, the gold is split according to the proposal. If not, the most experienced pirate is thrown off the ship and this process continues with the remaining pirates until a proposal is accepted. The first priority of the pirates is to stay alive and second to maximize the gold they get. Pirate 6 devises a plan which he knows will keep him alive. What is his plan?

Bulb or No Bulb?

There are 100 bulbs arranged in a row. Each bulb has its own switch and is currently turned off.  In the first round, you turn every switch on. In the second round, you flip the switch of every second bulb (i.e. bulb 2, 4, 6, 8 and so on). In the third round, you flip the switch of every third bulb and so on. What is the state of bulb 9 after 100 rounds? Also, how many bulbs are on after 100 rounds?

What’s Your Eye Color?

On a certain island there are people with assorted eye colors. There are 100 people with blue eyes and 100 people with brown eyes. Since there are no mirrors on this island, no person knows the color of their own eyes. The people on the island are not allowed to talk or communicate with each other in any way. They are also NOT aware of the number of blue or brown eyed people on the island. For all they know, they could have red eyes too. But they are allowed to observe other people and keep count of the number of people with a certain eye color. There is a rule that the people on the island have to follow – any person who is sure of their eye color has to leave the island immediately.

One day, an outsider comes to the island and announces to the people that he sees someone with blue eyes. What do you think happens?

How Strong is an Egg?

You have two identical eggs. Standing in front of a 100 floor building, you wonder what is the maximum number of floors from which the egg can be dropped without breaking it. What is the minimum number of tries needed to find out the solution?

Reverse a Linked List

Reverse a Linked list. Write code in C.

Is Your Husband a Cheat?

A certain town comprises of 100 married couples. Everyone in the town lives by the following rule: If a husband cheats on his wife, the husband is executed as soon as his wife finds out about him. All the women in the town only gossip about the husbands of other women. No woman ever tells another woman if her husband is cheating on her.  So every woman in the town knows about all the cheating husbands in the town except her own. It can also be assumed that a husband remains silent about his infidelity. One day, the mayor of the town announces to the whole town that there is at least 1 cheating husband in the town. What do you think happens?

Boys and Girls

In a country where everyone wants a boy, each family continues having babies till they have a boy. After some time, what is the proportion of boys to girls in the country? (Assuming probability of having a boy or a girl is the same)

Probability of a Car Passing By

The probability of a car passing a certain intersection in a 20 minute windows is 0.9. What is the probability of a car passing the intersection in a 5 minute window? (Assuming a constant probability throughout)

Sub-Array with the Largest Sum

You are given an array with integers (both positive and negative) in any random order. Find the sub-array with the largest sum.

How Old are My Children?

Two old friends, Jack and Bill, meet after a long time.

5 Pirates Fight for 100 Gold Coins

Five pirates discover a chest containing 100 gold coins. They decide to sit down and devise a distribution strategy. The pirates are ranked based on their experience (Pirate 1 to Pirate 5, where Pirate 5 is the most experienced). The most experienced pirate gets to propose a plan and then all the pirates vote on it. If at least half of the pirates agree on the plan, the gold is split according to the proposal. If not, the most experienced pirate is thrown off the ship and this process continues with the remaining pirates until a proposal is accepted. The first priority of the pirates is to stay alive and second to maximize the gold they get. Pirate 5 devises a plan which he knows will be accepted for sure and will maximize his gold. What is his plan?

Four People on a Rickety Bridge

Four people need to cross a rickety bridge at night. Unfortunately, they have only one torch and the bridge is too dangerous to cross without one. The bridge is only strong enough to support two people at a time. Not all people take the same time to cross the bridge. Times for each person:  1 min, 2 mins, 7 mins and 10 mins. What is the shortest time needed for all four of them to cross the bridge?

Apple and Oranges?

You have 3 baskets, one with apples, one with oranges and one with both apples and oranges mixed. Each basket is closed and is labeled with ‘Apples’, ‘Oranges’ and ‘Apples and Oranges’. However, each of these labels is always placed incorrectly. How would you pick only one fruit from a basket to place the labels correctly on all the baskets?

Trailing Zeros in 100 Factorial

How many zeros are there in 100! (100 factorial)?

Loop in a Singly-Linked List

How would you find a loop in a singly-linked list?

100 Prisoners in Solitary Cells

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 bulb. No other prisoners can see or control the light bulb. The prisoner in the special room can either turn on the bulb, turn off the bulb or do nothing. On any day, the prisoners can stop this process and say “Every prisoner has been in the special room at least once”. If that happens to be true, all the prisoners will be set free. If it is false, then all the prisoners will be executed. The prisoners are given some time to discuss and figure out a solution. How do they ensure they all go free?

4 Quarts of Water

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?

12 Identical Balls Problem

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.

A Box of Defective Balls

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?

8 Identical Balls Problem

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.

Little Endian or Big Endian?

Is your machine little or big endian? Write a function to determine that.

Gold for 7 Days of Work

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 is done during each day thus requiring equal amount of pay for each day)

Reverse the Order of Words in a String

Reverse the order of words in a string in C using as little additional memory as possible.

Reverse a String

Reverse a string in C using as little additional memory as possible.

Power of 2

How do you find out if an unsigned integer is a power of 2?

Get a free subscription to Oracle magazine published by Oracle Corp.
 Powered by Max Banner Ads 

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>