## Question

Given a list of items with values and weights, as well as a max weight, find the maximum value you can generate from items where the sum of the weights is less than the max.

eg.

1 2 3 |
items = {(w:1, v:6), (w:2, v:10), (w:3, v:12)} maxWeight = 5 knapsack(items, maxWeight) = 22 |

Once you think that you’ve solved the problem, click below to see the solution.

*As always, remember that practicing coding interview questions is as much about how you practice as the question itself. Make sure that you give the question a solid go before skipping to the solution. Ideally if you have time, write out the solution first by hand and then only type it into your computer to verify your work once you've verified it manually. To learn more about how to practice, check out this blog post.*

## Solution

How was that problem? You can check out the solution in the video below.

Here is the source code for the solution shown in the video (Github):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
// Item class public static class Item { int weight; int value; public Item(int weight, int value) { this.weight = weight; this.value = value; } } // Recursively check every combination of items by traversing list of items // and either including or excluding each item public static int naiveKnapsack(Item[] items, int W) { return naiveKnapsack(items, W, 0); } // Overloaded recursive function for naiveKnapsack private static int naiveKnapsack(Item[] items, int W, int i) { // Return when we reach the end of the list if (i == items.length) return 0; // If item is heavier than remaining weight, skip item if (W - items[i].weight < 0) return naiveKnapsack(items, W, i+1); // Try both including and excluding the current item return Math.max(naiveKnapsack(items, W - items[i].weight, i+1) + items[i].value, naiveKnapsack(items, W, i+1)); } // Recursive solution that uses a cache to improve performance public static int topDownKnapsack(Item[] items, int W) { // Map: i -> W -> value // Use a map instead of array because the data could be very sparse Map<Integer, Map<Integer, Integer>> cache = new HashMap<Integer, Map<Integer, Integer>>(); return topDownKnapsack(items, W, 0, cache); } // Overloaded recursive function for topDownKnapsack private static int topDownKnapsack(Item[] items, int W, int i, Map<Integer, Map<Integer, Integer>> cache) { // Return when we reach the end of the list if (i == items.length) return 0; // Check the cache and return value if we get a hit if (!cache.containsKey(i)) cache.put(i, new HashMap<Integer, Integer>()); Integer cached = cache.get(i).get(W); if (cached != null) return cached; // If item is heavier than remaining weight, skip item if (W - items[i].weight < 0) return topDownKnapsack(items, W, i+1, cache); // Try both including and excluding the current item int toReturn = Math.max(topDownKnapsack(items, W - items[i].weight, i+1, cache) + items[i].value, topDownKnapsack(items, W, i+1, cache)); cache.get(i).put(W, toReturn); return toReturn; } // Iterative dynamic programming solution public static int bottomUpKnapsack(Item[] items, int W) { // cache[i][j] = max value for the first i items with a max weight of j int[][] cache = new int[items.length + 1][W + 1]; for (int i = 1; i <= items.length; i++) { for (int j = 0; j <= W; j++) { // If including item[i-1] would exceed max weight j, don't // include the item. Otherwise take the max value of including // or excluding the item if (items[i-1].weight > j) cache[i][j] = cache[i-1][j]; else cache[i][j] = Math.max(cache[i-1][j], cache[i-1][j-items[i-1].weight] + items[i-1].value); } } return cache[items.length][W]; } |

Did you get the right answer to this coding interview question? Please share your thoughts in the comments below.