Three-Resource 0/1 Knapsack
Reported by candidates from Morgan Stanley's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Morgan Stanley reportedly served this one in September 2026, and it looks scarier than it is. Strip the wording and it's a plain 0/1 knapsack with two extra dimensions bolted on. Each item has a value and three costs, you pick each at most once, and you can't bust any of the three capacities. With up to 40 items and capacities up to 40, the state space is small enough to brute-force with DP. If you've seen classic knapsack, you've seen this. If you blank under the clock, StealthCoder is the quiet safety net running during the live OA.
The problem
Each item has a value and consumes three independent resources. Select each item at most once without exceeding any of the three capacities, and return the maximum total value. Function maximizeKnapsackValue(values: int[], firstCosts: int[], secondCosts: int[], thirdCosts: int[], firstCapacity: int, secondCapacity: int, thirdCapacity: int) → int Examples Example 1 values = [10,20] firstCosts = [1,2] secondCosts = [1,2] thirdCosts = [1,2] firstCapacity = 2 secondCapacity = 2 thirdCapacity = 2 return = 20 Case 1 exercises the documented deterministic contract. Example 2 values = [6,10,12] firstCosts = [1,2,3] secondCosts = [2,1,2] thirdCosts = [1,2,1] firstCapacity = 4 secondCapacity = 4 thirdCapacity = 3 return = 18 Case 2 exercises the documented deterministic contract. Example 3 values = [5] firstCosts = [0] secondCosts = [0] thirdCosts = [0] firstCapacity = 0 secondCapacity = 0 thirdCapacity = 0 return = 5 Case 3 exercises the documented deterministic contract. Constraints 1 <= values.length <= 40. All four item arrays have equal length. 0 <= costs and capacities <= 40. Values are nonnegative and the answer fits a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The reduction: dp[a][b][c] is the max value using at most a of resource one, b of resource two, c of resource three. For each item, loop a, b, c downward from the capacities to the item's costs and set dp[a][b][c] = max(dp[a][b][c], dp[a-c1][b-c2][c-c3] + value). Downward iteration is the whole trick, since it stops you reusing an item. Forward loops turn this into unbounded knapsack and you'll get wrong answers. Watch the zero-cost case in Example 3: an item costing nothing must still be counted once, and downward loops handle that if your bounds start at the cost, which is 0. Complexity is 40 items times 41 cubed, about 2.7 million operations, trivial. Initialize everything to 0, not negative infinity, because capacities mean at most. If you freeze on the live OA, StealthCoder is the hedge that reads the problem and hands you this loop structure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Three-Resource 0/1 Knapsack cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Morgan Stanley's OA.
Morgan Stanley reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Three-Resource 0/1 Knapsack FAQ
What's the trick in this Morgan Stanley knapsack problem?+
Treat it as 0/1 knapsack with a 3D table. dp[a][b][c] holds the best value within those three budgets. Iterate each capacity downward for every item so it's used at most once. That's the entire idea.
How hard is this really?+
Easier than it looks. If you know 1D 0/1 knapsack, you just add two nested loops. The hard part is remembering the reverse iteration order. Constraints are tiny, so no optimization is needed.
Why do I iterate backwards?+
Going backwards ensures dp entries you read still reflect the state before the current item. Going forwards lets the same item be counted multiple times, which turns it into unbounded knapsack and breaks Example 2.
What about items with zero cost?+
Example 3 covers it. A zero-cost item with value 5 and zero capacities returns 5. With downward loops starting at the item's cost, index 0 gets updated correctly. Initialize dp to 0 so unused capacity is fine.
How do I prepare in 48 hours for this pattern?+
Write 1D 0/1 knapsack from memory, then extend it to 2D and 3D. Check Example 2 by hand, expecting 18. Know the time cost: items times the product of capacities plus one, which is small here.