Time-Versioned Menu Price Store With Historical Lookups
Last updated: November 29, 2026
Quick Overview
Design a store where merchants set item prices over time with set_price(item, cents, ts), and support price_at(item, ts) returning the price that was in effect at any past timestamp.
DoorDash
November 29, 20260
0
0 solved
Design a store where merchants set item prices over time with set_price(item, cents, ts), and support price_at(item, ts) returning the price that was in effect at any past timestamp.
When a customer disputes a charge or support reprices a refund, DoorDash needs the menu price at the moment the order was placed, not today's price. This data structure question checks per-key sorted histories, binary search for the floor timestamp, and explicit rules for same-timestamp writes and out-of-order writes.
What the Interviewer Expects
- Store a per-item list of timestamps and prices
- Use bisect_right minus one to find the latest change at or before ts
- Define behavior for queries before the first price
- Decide how equal timestamps and backwards timestamps are handled
- Discuss persistence and how this maps to a database index
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 support out-of-order writes from offline merchant tablets?
- How would you compact history older than two years?
- How would you model this in SQL so the lookup is a single indexed query?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
set_price(item, cents, ts): timestamps are non-decreasing per item; an equal timestamp overwrites (a correction); a lower one is an error.price_at(item, ts): price from the latestset_price...
High-Level Design
Keep two dictionaries of parallel lists, times[item] and prices[item]. Appends keep times sorted, so a query is bisect_right(times, ts) - 1: the index of the last change at or before ts. An ...