Design an Inventory Counter With Expiring Cart Reservations
Last updated: November 29, 2026
Quick Overview
Implement an inventory structure where adding an item to a cart reserves units for a time-to-live, checkout confirms the reservation, and abandoned reservations automatically return stock when they expire. Support reserve, confirm, release and available queries with timestamps.
Walmart Labs
November 29, 20260
0
0 solved
Implement an inventory structure where adding an item to a cart reserves units for a time-to-live, checkout confirms the reservation, and abandoned reservations automatically return stock when they expire. Support reserve, confirm, release and available queries with timestamps.
Limited drops such as game consoles on Walmart.com produce huge spikes where overselling is unacceptable and abandoned carts must not lock stock forever. The Walmart Labs onsite interviewer checks clean invariants between on-hand, held and available stock, and efficient lazy expiry.
What the Interviewer Expects
- State the invariant available = on hand minus live holds
- Use a min-heap keyed by expiry for lazy cleanup
- Make confirm fail if the hold already expired
- Make release and confirm idempotent
- Analyze amortized cost per operation
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 make this safe across many servers sharing one inventory?
- How would you extend a hold while the shopper is still in checkout?
- What happens with clock skew between servers that set expiry times?
- How would you add per-customer purchase limits?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations, each with a now timestamp: reserve(sku, qty, now, ttl) returns an id or None if not enough is available; confirm(rid, now) turns a live hold into a sale; release(rid, now) cancels ...
High-Level Design
Track three things per SKU: on_hand (not yet sold) and held (sum of live holds), plus a map of holds by id. The invariant is available = on_hand - held >= 0. Expiry is lazy: a min-heap of `(expi...