Fewest Packs to Buy an Exact Quantity of a Bulk Item
Last updated: November 29, 2026
Quick Overview
A product is sold in several pack sizes with unlimited supply. Return the fewest packs that add up to exactly the requested quantity, along with the packs chosen, or report that the quantity cannot be made.
Walmart Labs
November 29, 20260
0
0 solved
A product is sold in several pack sizes with unlimited supply. Return the fewest packs that add up to exactly the requested quantity, along with the packs chosen, or report that the quantity cannot be made.
Framed around Sam's Club and Walmart.com bulk items sold in multiple pack sizes, where a shopper asks for an exact count. Walmart Labs phone screens use this to check whether a candidate sees that greedy fails and can write a bottom-up DP with reconstruction.
What the Interviewer Expects
- Show a counterexample where greedy by largest pack fails
- Define the DP state and transition clearly
- Initialize with infinity and a zero base case
- Reconstruct the chosen packs, not just the count
- Return a clear signal for impossible quantities
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you minimize price instead of pack count?
- What if each pack size has limited stock?
- How would you count the number of distinct ways to reach the quantity?
- Quantities can be up to 10 million; how would you reduce memory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: a list of positive pack sizes and a non-negative integer quantity. Output: (count, packs) where packs is the list of sizes used, or (-1, []) if no combination sums exactly. Quantity 0 needs...
High-Level Design
Greedy fails: with sizes [1, 3, 4] and quantity 6, greedy takes 4 + 1 + 1 (three packs) but 3 + 3 uses two. Define best[q] as the fewest packs summing to q. Then best[q] = 1 + min(best[q - s]) o...