LRU Quote Cache With Per-Entry Expiry for Yahoo Finance
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity cache for stock quotes where each entry has its own time-to-live. get returns a value only if it has not expired, put inserts or refreshes an entry, and when the cache is full it should evict an expired entry if one is cheaply available, otherwise the least recently used entry, all in O(1) amortized time.
Yahoo
November 29, 20260
0
0 solved
Implement a fixed-capacity cache for stock quotes where each entry has its own time-to-live. get returns a value only if it has not expired, put inserts or refreshes an entry, and when the cache is full it should evict an expired entry if one is cheaply available, otherwise the least recently used entry, all in O(1) amortized time.
Yahoo Finance quote pages fan out to many services, and quotes for active tickers go stale in seconds while reference data lives for hours. This design-a-data-structure question checks that you can build an LRU from a hash map plus doubly linked list and fold in TTL semantics without breaking O(1) operations.
What the Interviewer Expects
- Combine a hash map with a doubly linked list using sentinel nodes
- Treat expired entries as misses and delete them lazily on access
- Refresh both value and expiry on put of an existing key
- Bound the extra work during eviction so it stays O(1)
- Inject a clock so expiry is testable
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 thread-safe without a single global lock becoming a bottleneck?
- How would you proactively purge expired entries without scanning everything?
- How would you avoid a thundering herd when a hot quote like AAPL expires?
- How would this change if values have different memory sizes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
get(key) returns the value or None if missing or expired, and marks it most recently used. put(key, val, ttl) sets value and expiry now + ttl. Capacity is a count of entries. All operations sh...
High-Level Design
Classic LRU: a dict from key to node, and a doubly linked list ordered by recency with head and tail sentinels so unlink and push-front have no special cases.
TTL handling:
- Lazy expiry on
get...