Inventory Tracker With Expiring Checkout Holds
Last updated: November 29, 2026
Quick Overview
Design a class that tracks units on hand for items in one store and lets checkout place time-limited holds. Support restock, available, hold (returns an id or None), commit and release, with holds automatically expiring after a fixed TTL.
Instacart
November 29, 20260
0
0 solved
Design a class that tracks units on hand for items in one store and lets checkout place time-limited holds. Support restock, available, hold (returns an id or None), commit and release, with holds automatically expiring after a fixed TTL.
Instacart has to avoid selling the last carton of milk twice while orders sit in checkout. This onsite data structure question checks API design, lazy expiration with a heap, and correctness around holds that expire just as they are committed.
What the Interviewer Expects
- Separate on-hand units from held units so available = on_hand - held
- Expire holds lazily with a min-heap ordered by expiry time
- Make commit fail cleanly for expired or unknown hold ids
- Keep every operation O(log n) amortized
- Discuss how the single-process version maps to a distributed inventory service
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 let a hold be extended when the customer is still on the payment page?
- What changes when the store's real stock is only an estimate from the retailer feed?
- How would you make hold and commit safe across multiple servers?
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 take a now timestamp so the class is deterministic and testable. restock(item, qty, now), available(item, now), hold(item, qty, now) returning a hold id or None if not enough stock,...
High-Level Design
Keep on_hand[item] and held[item]. A dictionary holds maps id to (item, qty, expires_at) and a min-heap stores (expires_at, id). Every public call first pops expired entries from the heap an...