DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
DP

0/1 Knapsack

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **0/1 Knapsack** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** 2D DP table weight × items **Edge Cases to Consider:** - (1) Zero capacity - (2) All items exceed capacity - (3) Single item

Examples

Example 1
Input: [2,3,4,5], [3,4,5,6], 5
Output: 7
Explanation: Items 1+2 (w=2+3=5, v=3+4=7)
Example 2
Input: [1,2,3], [1,2,3], 5
Output: 5
Explanation: Items 2+3
Example 3
Input: [3,4,5], [4,5,6], 2
Output: 0
Explanation: Nothing fits

Constraints

  • ▪Input arguments are within valid ranges
  • ▪Optimize for execution speed
  • ▪Handle null/empty inputs gracefully

Watch Out For Edge Cases

  • ▪Zero capacity
  • ▪All items exceed capacity
  • ▪Single item
Frequently Asked At
AmazonGoogleMicrosoftTCSInfosysWipro