You are on page 1of 5

Brain Teasers for SDE Interviews

1.The Two Egg Problem


You are given two eggs, and you have access to a 100-story building. Your task is to find the highest
floor from which an egg will not break when dropped. You must minimize the number of egg drops
used to find the solution. The eggs are identical, and if an egg breaks when dropped from some floor,
it will break if dropped from a higher floor.

What is the minimum number of egg drops needed to find the highest floor from which an egg will
not break, and how would you go about achieving this?

Solution
To solve the Two Egg Problem with a minimum number of egg drops, you can use a binary search-
like approach to divide the building into segments and strategically drop the eggs. Here's one way to
do it:

1. Start by dropping the first egg from the 50th floor.


- If the egg breaks, you know the critical floor is somewhere between 1 and 49. You can now use the
second egg to perform a linear search within this range, starting from the first floor and moving
upward one floor at a time until the egg breaks. The total number of drops in this case would be 50.

- If the egg doesn't break, you know the critical floor is somewhere between 51 and 100. You can
repeat the process by dropping the second egg from a higher floor, such as the 75th floor. Depending
on whether the egg breaks or not, you can further narrow down the range of possible critical floors.
The total number of drops in this case would be 2 (first egg) + X (linear search using the second egg,
where X is the number of drops required to find the critical floor within the remaining range).

By using this approach, you can guarantee finding the critical floor with a maximum of 50 + X egg
drops, where X is the number of drops required for the linear search. This method is designed to
minimize the worst-case scenario.

Keep in mind that the goal is to optimize the worst-case scenario, as the critical floor could be at the
very top of the building, and you want to minimize the number of eggs drops regardless of where the
critical floor is located.
2.The Bridge and Torch Problem

You are given a torch that can last for exactly 17 minutes, and you are standing on one side of a
bridge. On the other side of the bridge, there are four people who need to cross. The bridge can only
hold a maximum of two people at a time, and the group must use the torch to cross. The four people
walk at different speeds: one can cross the bridge in 1 minute, another in 2 minutes, another in 5
minutes, and the slowest takes 10 minutes.
When two people cross the bridge together, they must go at the slower person's pace. The torch cannot
be thrown across the bridge – it must be carried back and forth. You need to devise a plan to get all
four people across the bridge in 17 minutes or less.

What is your strategy to achieve this goal?

Solution
Certainly! Here's the strategy to solve the Bridge and Torch Problem within the given constraints:

Step 1: The two fastest people (1-minute and 2-minute) cross the bridge together, taking 2 minutes.

Step 2: The fastest person (1-minute) returns with the torch, taking 1 minute. Now, there are two
people on the initial side.

Step 3: The two slowest people (10-minute and 5-minute) cross the bridge together, taking 10
minutes.

Step 4: The second-fastest person (2-minute) returns with the torch, taking 2 minutes. Now, there is
one person on the initial side.

Step 5: The fastest person (1-minute) crosses the bridge again, taking 1 minute.

In total, the time taken for all four people to cross the bridge is 2 + 1 + 10 + 2 + 1 = 16 minutes.

This strategy optimizes the time by making sure the slowest people only cross the bridge together, and
the fastest people are utilized to minimize the time taken for each crossing. The key is to leverage the
fact that when two people cross, they must go at the slower person's pace, so it's important to ensure
that the slowest people are not paired with the fastest ones for any given crossing.
3.The 3-Liter and 5-Liter Water Jug Problem

You are given two empty jugs: one that can hold 3 liters of water and another that can hold 5 liters of
water. You also have access to a water source. Your goal is to measure exactly 4 liters of water using
these two jugs, without any markings on them to indicate volume.

Here are the rules:

1. You can fill a jug completely from the water source.


2. You can empty a jug completely.
3. You can pour the water from one jug into the other until the first jug is either empty or the second
jug is full.

How can you measure exactly 4 liters of water using these two jugs?

This brain teaser challenges your ability to think creatively and come up with a sequence of actions
that utilize the jugs to achieve a specific volume of water. It also requires you to consider the
interactions between the jugs and how their capacities can be combined to reach the desired
measurement.

Solution
Certainly! Here's a step-by-step solution to measure exactly 4 liters of water using the 3-liter and 5-
liter jugs:

Step 1: Fill the 3-liter jug to the top.


Step 2: Pour the water from the 3-liter jug into the 5-liter jug.
- 3-liter jug: 0 liters
- 5-liter jug: 3 liters

Step 3: Fill the 3-liter jug to the top again.


Step 4: Carefully pour the water from the 3-liter jug into the 5-liter jug until the 5-liter jug is full. This
leaves you with 1 liter of water in the 3-liter jug.
- 3-liter jug: 1 liter
- 5-liter jug: 5 liters (full)
Step 5: Empty the 5-liter jug.
- 3-liter jug: 1 liter
- 5-liter jug: 0 liters

Step 6: Carefully pour the remaining water from the 3-liter jug into the empty 5-liter jug.
- 3-liter jug: 0 liters
- 5-liter jug: 1 liter

Step 7: Fill the 3-liter jug again.


- 3-liter jug: 3 liters
- 5-liter jug: 1 liter

Step 8: Carefully pour water from the 3-liter jug into the 5-liter jug until the 5-liter jug is full. This
will leave you with 4 liters of water in the 3-liter jug.
- 3-liter jug: 4 liters
- 5-liter jug: 5 liters (full)

Now you have successfully measured exactly 4 liters of water using the 3-liter and 5-liter jugs. This
solution utilizes the interactions between the two jugs and the various actions you can take to
manipulate the water levels and achieve the desired measurement.
4.The Poisoned Wine Problem

You are a king who suspects that one of your advisors is trying to poison you. You have 1,000 bottles
of wine, and you know that exactly one of them is poisoned. The poison takes effect within 24 hours.
Fortunately, you have 10 prisoners who are sentenced to death, and you can use them to help
determine which bottle is poisoned.
Here's the catch: The poison is so potent that a drop of it will kill the drinker within 24 hours. You
can't taste or smell the poison, and any amount of poison will kill. The prisoners can only drink once.
How can you determine which bottle is poisoned within 24 hours, using only the 10 prisoners and the
bottles of wine?

Solution

Step 1: Number the bottles of wine from 1 to 1000.

Step 2: Assign each prisoner a unique number from 1 to 10.

Step 3: Use binary representation to represent the bottle numbers. For example, if you want to
represent bottle number 49, it would be 110001 in binary (since 49 is 110001 in binary).

Step 4: Each prisoner drinks from the bottles whose corresponding digits are 1 in their binary
representation. For example, Prisoner 1 drinks from all the bottles where the least significant digit is 1
(i.e., bottles ending in 1 in binary), Prisoner 2 drinks from bottles where the second least significant
digit is 1 (i.e., bottles where the second digit from the right is 1), and so on.

Step 5: After 24 hours, the prisoners who are alive will help identify the poisoned bottle. The binary
representation of their numbers will indicate the poisoned bottle.

For example, if Prisoners 1, 3, and 4 are alive, their binary representations are 001, 101, and 100,
respectively. Combine their digits to get 001101100 in binary, which is 108 in decimal. This identifies
the poisoned bottle.

By using this strategy, you can determine the poisoned bottle within 24 hours using the 10 prisoners
and the bottles of wine. This approach leverages the unique binary representation of each prisoner's
number to encode information about the bottles they drank from, allowing you to identify the
poisoned bottle based on which prisoners are still alive.

You might also like