Bird
Raised Fist0

Consider the following code for integer break. What is the value of dp[3] after the outer loop iteration i = 3 completes?

easy🧾 Code Trace Q12 of Q15
Dynamic Programming: Knapsack - Integer Break
Consider the following code for integer break. What is the value of dp[3] after the outer loop iteration i = 3 completes?
def integer_break(n):
    dp = [0] * (n + 1)
    dp[1] = 1
    for i in range(2, n + 1):
        max_product = 0
        for j in range(1, i):
            max_product = max(max_product, max(j, dp[j]) * max(i - j, dp[i - j]))
        dp[i] = max_product
    return dp[n]

print(integer_break(4))  # Output not asked here
A2
B4
C3
D1
Step-by-Step Solution
  1. Step 1: Trace dp for i=2

    dp[1] = 1 (given). For i=2, j=1: max_product = max(0, max(1,1)*max(1,1)) = 1, so dp[2] = 1.
  2. Step 2: Trace dp for i=3

    j=1: max_product = max(0, max(1,1)*max(2,dp[2])) = max(0,1*2) = 2 j=2: max_product = max(2, max(2,dp[2])*max(1,dp[1])) = max(2,2*1) = 2 So dp[3] = 2.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    dp[3] = 2 matches manual calculation [OK]
Quick Trick: Trace inner loop carefully for each j [OK]
Common Mistakes:
MISTAKES
  • Off-by-one in loop bounds
  • Confusing dp[i-j] with dp[j]
Trap Explanation:
PITFALL
  • Candidates often miscalculate max_product by mixing indices or skipping max(j, dp[j]) logic.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute DP code and track intermediate states
Master "Integer Break" in Dynamic Programming: Knapsack

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Dynamic Programming: Knapsack Quizzes