← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Senior

Senior
May 2026

Summary

Google SWE interview that went deep on dynamic programming, specifically knapsack variants. They wanted both implementations and a dry-run on actual inputs, which I was not fully expecting.

Questions Asked (3)

Q1

Implement the 0/1 knapsack problem: given item weights, values, and a capacity W, return the maximum value achievable and reconstruct the actual set of items chosen. Provide both the full O(nW) time and space DP table solution and a space-optimized O(W) space version.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The two-table vs rolling-array distinction is where I fumbled a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the DP state and recurrence for the 0/1 knapsack, then implement the full O(nW) table to reconstruct the chosen items. After that, explain the space-optimized O(W) version, noting that reconstruction requires either storing parent pointers or re-running the DP with a copy of the previous row.

Pro tip: Emphasize that the space-optimized version cannot reconstruct the solution without additional storage; discuss trade-offs between storing decisions (O(nW) bits) or re-computing (O(nW) time). This shows you understand practical constraints beyond just the algorithm.

1. Clarify problem and constraints

Confirm that each item can be taken at most once (0/1), and ask about input sizes, value ranges, and whether reconstruction is required. This helps decide between DP approaches.

2. Define DP state and recurrence

Let dp[i][w] be the max value using first i items with capacity w. Recurrence: dp[i][w] = max(dp[i-1][w], dp[i-1][w-w_i] + v_i) if w_i <= w, else dp[i-1][w].

3. Implement full DP table and reconstruction

Fill the (n+1) x (W+1) table. To reconstruct, backtrack from dp[n][W]: if dp[i][w] != dp[i-1][w], item i was taken; update w -= w_i and i--. Otherwise, i--.

4. Optimize space to O(W)

Use a 1D array dp[w] and iterate items, updating w from W down to w_i to avoid reusing items. For reconstruction, either keep a 2D boolean array of decisions or re-run the DP with a copy of the previous row.

5. Analyze complexity and trade-offs

Full DP: O(nW) time and space. Optimized: O(nW) time, O(W) space, but reconstruction needs extra O(nW) bits or O(nW) time. Discuss when each is preferable.

Key Points to Mention

  • DP state definition and recurrence relation
  • Base cases: dp[0][w] = 0 and dp[i][0] = 0
  • Reconstruction method by backtracking through the DP table
  • Space optimization using a 1D array and iterating weights in reverse
  • Trade-offs: full table allows easy reconstruction; optimized space requires extra storage or recomputation
  • Time and space complexity analysis for both versions

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Extend the 0/1 knapsack to a bounded variant where each item has a maximum count it can be used. Implement an efficient O(nW) solution using binary decomposition of item counts, and walk through the complexity and memory trade-offs.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Binary decomposition clicked for me during prep so I could explain it: you split count[i] into powers of two as virtual items, then run standard 0/1 knapsack on the expanded list.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the bounded knapsack problem and why the naive O(nW * count) approach is inefficient. Then describe binary decomposition: split each item's count into powers of two (1, 2, 4, ...) to create new items with weight and value multiplied by the power, reducing the problem to 0/1 knapsack. Finally, analyze the O(nW) time complexity after decomposition and discuss memory trade-offs between 1D and 2D DP arrays.

Pro tip: Emphasize that binary decomposition reduces the number of items to O(log count) per original item, making the solution efficient even for large counts. Also, mention that using a 1D DP array saves memory but requires iterating weights in reverse to avoid reusing items.

1. Clarify the problem and constraints

Restate the bounded knapsack: each item has a weight, value, and maximum count. Confirm that the goal is to maximize value without exceeding capacity W, and that counts are integers.

2. Explain binary decomposition

For each item with count c, decompose c into powers of two (e.g., 1, 2, 4, ..., remainder). Create new items with weight and value multiplied by these powers, effectively converting the bounded problem into a 0/1 knapsack with O(n log c) items.

3. Apply 0/1 knapsack DP

Use dynamic programming: dp[j] = max value for capacity j. For each new item, iterate j from W down to weight to avoid reusing items. This yields O(nW) time after decomposition.

4. Analyze complexity and memory

Time: O(nW) where n is the number of items after decomposition (original n times log of max count). Space: O(W) with 1D DP, or O(nW) with 2D DP. Discuss trade-offs: 1D saves memory but loses reconstruction ability; 2D allows backtracking but uses more memory.

5. Discuss optimizations and edge cases

Mention that if counts are large, binary decomposition is optimal. Also, handle cases where item weight exceeds capacity (skip) and ensure integer overflow is considered for large values.

Key Points to Mention

  • Binary decomposition reduces item count from c to O(log c) per item, making the DP efficient.
  • The transformed problem is a 0/1 knapsack, solvable in O(nW) time where n is the number of decomposed items.
  • Using a 1D DP array reduces space to O(W) but requires reverse iteration over capacities.
  • Time complexity after decomposition is O(nW) where n is original number of items times log(max count).
  • Memory trade-off: 1D DP is space-efficient but cannot reconstruct the solution; 2D DP allows backtracking at O(nW) space.
  • Edge cases: items with weight > W are ignored; counts of zero are skipped; large values may need 64-bit integers.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Dry-run both the 0/1 and bounded knapsack solutions on the input: weights=[2,3,4], values=[4,5,10], capacity=7 for the 0/1 case, then add counts=[3,1,2] for the bounded case. Trace through the DP table step by step.

Algorithms & Data Structures
Author's notes

Honestly the dry-run part was fine for 0/1.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clearly define the DP state and recurrence for both 0/1 and bounded knapsack. Then, systematically fill the DP table row by row, showing intermediate values and explaining the decisions at each cell. Finally, extract the optimal value and backtrack to identify the selected items.

Pro tip: Emphasize the difference in state transitions: 0/1 uses a 1D array iterated backwards to avoid reusing items, while bounded knapsack can be optimized with binary splitting or monotonic queue. Mentioning these optimizations shows depth.

1. Define DP state and recurrence

State that dp[i][w] represents the maximum value using first i items with capacity w. For 0/1, dp[i][w] = max(dp[i-1][w], dp[i-1][w-weight[i]] + value[i]) if weight[i] <= w. For bounded, include count k: dp[i][w] = max_{0<=k<=count[i], k*weight[i]<=w} (dp[i-1][w - k*weight[i]] + k*value[i]).

2. Initialize the DP table

Create a table with rows for items 0 to n and columns for capacities 0 to W. Set dp[0][w] = 0 for all w, and dp[i][0] = 0 for all i.

3. Fill the table for 0/1 knapsack

For each item i from 1 to 3, and each capacity w from 0 to 7, compute dp[i][w] using the recurrence. Show the table after each item, highlighting the decision (include or exclude).

4. Fill the table for bounded knapsack

Using the same items but with counts [3,1,2], for each item i and capacity w, consider taking k copies (0 <= k <= count[i]) and compute the maximum. Show the updated table.

5. Extract optimal value and backtrack

Read the final answer from dp[n][W]. Then backtrack through the table to determine which items (and how many) were selected, explaining the choices.

Key Points to Mention

  • DP state definition and recurrence relation for both variants
  • Difference between 0/1 and bounded knapsack: item availability (1 vs multiple copies)
  • Time and space complexity: O(nW) for 0/1, O(nW * max_count) for naive bounded, and optimizations like binary splitting or monotonic queue
  • Handling of base cases: dp[0][w] = 0 and dp[i][0] = 0
  • Backtracking to find the actual items selected, not just the maximum value
  • Potential optimizations: using 1D array for 0/1 (iterating backwards) and for bounded knapsack

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.