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

Coding Interview Essentials Β· Recursion and Dynamic Programming Problems Β· question 74 of 120

Can you solve the knapsack problem using dynamic programming?

πŸ“• Buy this interview preparation book: 120 Coding Interview Essentials questions & answers β€” PDF + EPUB for $5

The knapsack problem can be solved using Dynamic Programming. The knapsack problem is a problem in combinatorial optimization. Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

Let’s say you are given a set of β€˜nβ€˜ items, each item β€˜iβ€˜ has some weight β€˜w[i]β€˜ and a value β€˜v[i]β€˜. You are also given a maximum weight β€˜Wβ€˜. The goal is to choose items with maximum total value such that their total weight is not more than β€˜Wβ€˜.

We can define a 2D table β€˜dp[i][j]β€˜ where β€˜iβ€˜ ranges from 0 to β€˜nβ€˜ (the item index) and β€˜jβ€˜ ranges from 0 to β€˜Wβ€˜ (the maximum weight). Each cell β€˜dp[i][j]β€˜ represents the maximum value we can get by considering the first β€˜iβ€˜ items and a maximum weight of β€˜jβ€˜.

The base case for this problem is β€˜dp[i][0]β€˜ and β€˜dp[0][j]β€˜, which represent the cases where the maximum weight is 0 (we can’t choose any items) and where we don’t consider any items, respectively. In both cases, the maximum value is 0.

Then, for filling in the rest of the table, we follow this recursive relation:

if w[i] > j :
    dp[i][j] = dp[i-1][j]
else:
    dp[i][j] = max(dp[i-1][j], v[i] + dp[i-1][j-w[i]])

Explicitly, this recursive relation means that, if the weight of the current item is more than the current maximum weight, we can’t include this item, so the maximum value is the same as the maximum value not considering this item. Otherwise, the maximum value is either the maximum value not considering this item, or the value of this item plus the maximum value we can get considering the remaining weight.

Finally, β€˜dp[n][W]β€˜ will give the maximum total value including the first β€˜nβ€˜ items for a maximum weight β€˜Wβ€˜.

The pseudo code for the DP approach:

function knapSack(W, w, v, n):
   K = array of [n+1][W+1] 
   
   // Build table K[][] in bottom up manner
   for i from 0 to n:
       for w from 0 to W:
           if i==0 or w==0:
               K[i][w] = 0
           else if w[i-1] <= w:
               K[i][w] = max(v[i-1] + K[i-1][w-w[i-1]],  K[i-1][w])
           else:
               K[i][w] = K[i-1][w]
 
   return K[n][W] // this will return the maximum value that can be put in the knapsack of capacity W

This dynamic programming solution for the knapsack problem has a time complexity of β€˜O(nW)β€˜ and a space complexity of β€˜O(nW)β€˜, where β€˜nβ€˜ is the number of items and β€˜Wβ€˜ is the maximum weight. This is because it fills up a 2D table of size β€˜nWβ€˜ with the maximum values for each subproblem.

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

All 120 Coding Interview Essentials questions Β· All topics