DSA interview question
You're building the resource-allocation engine for a cloud infrastructure procurement tool. The company can buy server "packages" — each package has a fixed cost (weight, in terms of budget) and delivers a certain compute value. Unlike a one-time purchase decision, any package type can be bought as many times as you want (there's no limit on how many identical servers you can order). Given a total budget W (knapsack capacity), and arrays of weight[] and value[] for each item type, find the maximum total value achievable without exceeding the budget.
int unboundedKnapsack(vector<int>& weight, vector<int>& value, int W);
weight | value | W | Output | Explanation |
|---|---|---|---|---|
[1,3,4,5] | [10,40,50,70] | 8 | 110 | two items of weight 4 (value 50 each) → 100... verify: actually 3+5=8 → 40+70=110 |
[5,10,15] | [10,30,20] | 100 | 300 | ten items of weight 10 (value 30 each) → 300 |
[1] | [10] | 5 | 50 | five items of weight 1 → 50 |
[3] | [10] | 4 | 10 | can only fit one item of weight 3 (leaves 1 wasted capacity) |
[] | [] | 10 | 0 | no items available |
[5] | [10] | 3 | 0 | item doesn't fit at all within capacity |
[2,4,6] | [3,7,10] | 8 | 12 | two items of weight 4 → 14... verify via code, or 4+4=8→14 or other combos |
(Always verify tricky test cases with actual code execution before quoting — unbounded knapsack combinatorics can be non-obvious by hand.)
W guaranteed non-negative? Are weight/value arrays guaranteed same length?W and number of item types? (Signals whether O(n·W) DP is acceptable.)On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in