From 0/1 knapsack to bounded knapsack
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Sep 2025. It tests dynamic programming.
Start with the 0/1 knapsack: items with weights and values, a capacity W, each item used at most once. Return the best total value and one set of items that achieves it. Then let each item be used up to a given count, and extend the solution efficiently.
Part 1: 0/1 knapsack with the chosen items
-
knapsack_01(weight: list[int], value: list[int], W: int) -> tuple[int, list[int]]— the maximum value with total weight<= W, and the chosen indices in ascending order.
Give the O(n·W) time, O(n·W) space table first (it allows reconstruction), then the O(W) space version of the value alone, and explain what reconstruction needs in that case.
knapsack_01([2, 3, 4], [4, 5, 10], 7) -> (15, [1, 2]) # weights 3 + 4
knapsack_01([1, 2, 3], [6, 10, 12], 5) -> (22, [1, 2])
Part 2: bounded knapsack
-
knapsack_bounded(weight, value, count, W) -> int— itemimay be used up tocount[i]times.
Give the naive version (treat each copy as its own item) and an efficient one: split each count into powers of two (1, 2, 4, …, remainder), so any number of copies from 0 to count[i] is a subset of the chunks, then run the 0/1 DP over the chunks.
knapsack_bounded([2, 3, 4], [4, 5, 10], [3, 1, 2], 7) -> 15 # one of item 1, one of item 2
knapsack_bounded([2, 3, 4], [4, 5, 10], [3, 1, 2], 8) -> 20 # two of item 2; the 0/1 answer would be 15
Constraints
-
0 <= n <= 1000,0 <= W <= 10^4, weights, values and counts non-negative.
Clarifying questions
- Can weights be zero? (A zero-weight item with positive value is always taken.)
- Must the knapsack be exactly full, or at most
W? - Do you need the chosen items or only the value?
- How large are the counts? That decides whether the naive expansion is fine.
What a strong answer covers
- The recurrence
dp[i][w] = max(dp[i-1][w], dp[i-1][w - weight[i-1]] + value[i-1]), and reconstruction by walking back wheredp[i][w] != dp[i-1][w]. - The one-row version iterates
wfromWdown so each item is used once; iterating upward gives the unbounded knapsack instead. - Binary splitting gives O(W · Σ log count[i]) time; mention the monotonic-queue method that reaches O(n · W).
- Dry-running both examples by hand.
- Tests:
W = 0, an item heavier thanW, zero counts, zero weights.
Follow-ups
- Reconstruct the chosen copies in the bounded version.
- Unbounded counts (each item any number of times).
-
Wis 10^9 but values are small: switch the DP to "minimum weight for each value".
Where it's asked
- Google · Onsite coding · Last asked Sep 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
