WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Dynamic Programming Β· Intermediate Β· question 30 of 100

Solve the Egg Dropping Puzzle using dynamic programming.?

πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

The Egg Dropping Puzzle is a classic problem in dynamic programming, which aims to find the minimum number of trials required to determine the height at which an egg will break when dropped from a building. In this problem, we are given the number of eggs and the number of floors of a building, and we need to determine the minimum number of trials required to determine the exact floor from which the egg will break when dropped.

The brute force approach to solve this problem is to try dropping the egg from all the floors one by one, starting from the first floor and moving up, until the egg breaks. However, this approach can be very inefficient, especially for taller buildings.

To solve this problem efficiently using dynamic programming, we can break it down into smaller subproblems and use memoization to store the results of the subproblems to avoid redundant calculations.

Let’s denote the number of eggs by β€˜nβ€˜ and the number of floors by β€˜kβ€˜. We can define the state of the problem as β€˜(n, k)β€˜, which means that we have β€˜nβ€˜ eggs and β€˜kβ€˜ floors remaining to consider. The goal is to find the minimum number of trials required to determine the exact floor from which the egg will break when dropped.

Let β€˜dp(n, k)β€˜ be the minimum number of trials required to determine the exact floor, given β€˜nβ€˜ eggs and β€˜kβ€˜ floors. At each floor β€˜iβ€˜, we can either drop the egg or not drop the egg. If we drop the egg, then we need to consider the floors below β€˜iβ€˜ (i.e., β€˜dp(n - 1, i - 1)β€˜), since the egg will break if dropped from floor β€˜iβ€˜. If we don’t drop the egg, then we need to consider the floors above β€˜iβ€˜ (i.e., β€˜dp(n, k - i)β€˜), since the egg will not break if dropped from floor β€˜iβ€˜.

Therefore, the optimal solution for β€˜dp(n, k)β€˜ can be computed as follows:

public static int eggDrop(int n, int k) {
    int[][] dp = new int[n + 1][k + 1];
    
    // Base case 1: If there are no floors or only one floor, then we need one trial.
    for (int i = 1; i <= n; i++) {
        dp[i][0] = 0;
        dp[i][1] = 1;
    }
    
    // Base case 2: If there is only one egg, we need to try every floor one by one
    for (int j = 1; j <= k; j++) {
        dp[1][j] = j;
    }
    
    // Fill the rest of the table using the optimal substructure
    for (int i = 2; i <= n; i++) {
        for (int j = 2; j <= k; j++) {
            dp[i][j] = Integer.MAX_VALUE;
            for (int x = 1; x <= j; x++) {
                int maxDrops = 1 + Math.max(dp[i-1][x-1], dp[i][j-x]);
                dp[i][j] = Math.min(dp[i][j], maxDrops);
            }
        }
    }
    
    return dp[n][k];
}

Let’s break down the above code to understand it better.

First, we create a 2D array β€˜dpβ€˜ of size β€˜(n+1) x (k+1)β€˜ to store the minimum number of trials required for each state.

Next, we set the base cases. If there are no floors or only one floor, then we need one trial, regardless of the number of eggs we have. If we have only one egg, we need to try every floor one by one.

Then, we use the optimal substructure to compute the optimal solution for β€˜dp(n,k)β€˜. For each egg β€˜iβ€˜ and each floor β€˜jβ€˜, we try dropping the egg from all the floors one by one, starting from the first floor and moving up, until the egg breaks. For each try, we compute the maximum number of trials required, taking into account the worst case scenario where the egg breaks or doesn’t break. Finally, we choose the minimum number of trials required among all the floors for the current state.

After we have filled the entire table, the value in the last cell β€˜dp(n,k)β€˜ gives us the minimum number of trials required to determine the exact floor.

Overall, the time complexity of the above code is O(nk2), since we are filling up a 2D array of size β€˜(nβ€…+β€…1)x(kβ€…+β€…1)β€˜ using nested loops. However, the space complexity can be reduced to O(k) by using two 1D arrays, as we only need to store the previous and current rows of the table, not the entire table.

Example: Let’s say we have 2 eggs and a building with 10 floors. The output will be β€˜4β€˜, which means that the minimum number of trials required to determine the exact floor from which the egg will break when dropped is β€˜4β€˜. Here is an illustration of the optimal solution for this example:

   10
   
   
   9
   
   8
   
   7
   
   6
   
   5
   
   4
   
   3
   
   2
   
   1
   

We drop the first egg from the 4th floor, since it gives us the maximum coverage of floors (i.e., 4 floors below and 5 floors above). If the egg doesn’t break, we then drop the second egg from floor 8, as we need to cover only the remaining floors above floor 4. If the first egg breaks, we drop the second egg from the 1st floor, since we need to cover only the remaining floors below floor 4.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview β€” then scores it.
πŸ“ž Practice Dynamic Programming β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

All 100 Dynamic Programming questions Β· All topics