Enumerate Gift Baskets That Hit an Exact Budget

Last updated: November 29, 2026

Quick Overview

Given item prices (prices can repeat because different items can cost the same), list every distinct basket of items whose prices sum exactly to a budget, using each item at most once. Baskets with the same multiset of prices count once.

Walmart Labs
Coding & Algorithms
Software Engineer
Walmart Labs
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given item prices (prices can repeat because different items can cost the same), list every distinct basket of items whose prices sum exactly to a budget, using each item at most once. Baskets with the same multiset of prices count once.

A backtracking question framed around Walmart gift registry and holiday gift card bundles. The interviewer checks that you prune with sorting and skip duplicate values at the same recursion depth so the output has no repeated baskets.

What the Interviewer Expects
  • Sort prices to enable pruning and duplicate skipping
  • Advance the start index so each item is used once
  • Skip equal values at the same depth, not across depths
  • Break early when a price exceeds the remaining budget
  • Explain the exponential worst case
Key Topics to Cover
backtracking
pruning
duplicate handling
combinations
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return only the count of baskets, and can that be done faster?
  • What if each basket must hold at most four items?
  • How would you return baskets as item names rather than prices?
  • How do you bound the runtime if the catalog has thousands of items?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a list of positive integer prices in cents and a budget. Output: every distinct combination, as a sorted list of prices, summing exactly to the budget. Each list entry is one item and can be us...

High-Level Design

Depth first search over sorted prices. At each depth we choose the next item from index start onward and recurse with i + 1, which enforces single use. The duplicate rule: at a given depth, if `pr...


Submit Your Answer
Markdown supported

Related Questions