Algorithms: Puzzle called “Falling egg”

Assume you are given 2 eggs.
You have access to a 100-floors high building
Eggs can break if dropped from the first floor or may not even break if dropped from 100th floor.
Both eggs are identical.

You need to figure out the highest floor of a 100-storey building an egg can be dropped from without breaking.
The question is how many drops you need to make. You are allowed to break 2 eggs in the process

The following is a description of the instance of this famous puzzle involving
– N = 2 eggs
– building with H = 100 floors

Suppose that we wish to know which floors in a 100-floors high building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions:
– An egg that survives a fall can be used again.
– A broken egg must be discarded.
– The effect of a fall is the same for all eggs.
– If an egg breaks when dropped, then it would break if dropped from a higher window.
– If an egg survives a fall, then it would survive a any shorter fall.

It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 100th-floor windows do not cause an egg to break.

If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way:
1. drop the egg from the first-floor window
2. if it survives, drop it from the second-floor window
3. Continue upward until it breaks

In the worst case, this method may require 100 droppings.

But according to our scenario we have 2 eggs available.
What is the least number of egg-droppings that is guaranteed to work in all cases?

To derive a dynamic programming functional equation for this puzzle, let the state of the dynamic programming model be a pair s = (n,k), where
n = number of test eggs available, n = 0, 1, 2, 3, …, N – 1.
k = number of (consecutive) floors yet to be tested, k = 0, 1, 2, …, H – 1.

For instance, s = (3,48) indicates that
– 3 test eggs are available
– 48 (consecutive) floors are yet to be tested

The initial state of the process is s = (N,H) where N denotes the number of test eggs available at start of the experiment.
The process terminates either when
– there are no more test eggs (n = 0) or
– when k = 0, whichever occurs first.
If termination occurs at state s = (0,k) and k > 0, then the test failed.

Now, let function W(n,k) = minimum number of trials required to identify the value of the critical floor under the worst case scenario given that the process is in state s = (n,k).

Then it can be shown that
W(n,k) = 1 + min{max(W(n – 1, x – 1), W(n,k – x)): x = 1, 2, …, k;}
with knowing that W(n,1) = 1 for all n > 0 and W(1,k) = k for all k.

It is easy to solve this equation iteratively by systematically increasing the values of x.

In the following code there is my own generalized solution for N eggs and K-floors high building coded in C++. I know it’s not optimal, but it works and gives correct results.

using namespace std;

struct Status {
  int n; 
  int k;
  bool operator < (const Status& other) const {
    return n < other.n ? true : k < other.k;

  Status(int n, int k):n(n), k(k){}
map<string, int> W_res;

int GetMax (int a, int b) {
  return a>b?a:b;

int W(Status status, int passed = 0){
  To Avoid computing the same again ->
  use precomputed values
  if(W_res.find(status) != W_res.end()) {
    return W_res[status];

  if(status.n == 0 && status.k > 0) throw "Wrong way";
  else if(status.n == 1 || status.k <= 1) return status.k;
  else {
    int min = status.k;
    int max_x;
    for(int x = 1; x <= status.k; x++) {
        int max = GetMax(W(Status(status.n-1, x-1), passed), W(Status(status.n, status.k-x), x));
        if(max < min) {
          min = max;
          max_x = x + passed;
      }catch (const char* err_msg){;}//ignore this result

    W_res[status] = 1 + min; //save as precomputed value
    return 1 + min;

int main (int argc, char *argv[]) {
  cout << "Please enter number of floors: ";
  int NUM_FLOORS = 0;
  cin >> NUM_FLOORS;

  cout << "Please enter number of available eggs: ";
  int NUM_EGGS = 0;
  cin >> NUM_EGGS;

  clock_t start = clock();

  cout << "Number of floors: " << NUM_FLOORS << endl
       << "Number of available eggs: " << NUM_EGGS << endl << endl
       << "How many attempts do we need at worst case " << endl
       << "to check the highest floor we can drop an egg from " << endl
       << "without breaking it: " << W(Status(NUM_EGGS,NUM_FLOORS)) << endl;

  double duration = double((clock() - start)) / CLOCKS_PER_SEC;
  cout << "\nTime needed for computation (sec): " << duration 
       << "\n\nPress Enter to termiate";


Leave a Reply