Can you explain the 0-1 Knapsack problem and its solution?

DSA
Hard
Infosys
141.5K views

The 0-1 Knapsack problem involves selecting items with given weights and values to maximize total value without exceeding a weight capacity.

Why Interviewers Ask This

This is a quintessential dynamic programming problem that tests optimization skills. Interviewers assess your ability to define states and transitions. It also evaluates your understanding of NP-hard problems and approximation strategies.

How to Answer This Question

Define the problem clearly with items, weights, values, and capacity. Explain the recursive relation: max value is either including the item or excluding it. Show how to build a DP table to store results of subproblems. Discuss the time and space complexity and mention space optimization techniques.

Key Points to Cover

  • Dynamic programming table
  • Include vs exclude decision
  • State transition formula
  • O(n*W) complexity

Sample Answer

The 0-1 Knapsack problem is solved using dynamic programming. We create a 2D table where dp[i][w] represents the max value using the first i items with capacity w. For each item, we decide whether to include it (if weigh…

Common Mistakes to Avoid

  • Confusing with fractional knapsack
  • Incorrect base case initialization
  • Not handling weight constraints properly

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 107 DSA questionsBrowse all 100 Infosys questions