Design a Data Structure for Stock Prices (Heaps)
Design a data structure that allows you to record the latest stock price and retrieve the maximum, minimum, and current price in $O(1)$ time. Use two heaps and a hash map.
Why Interviewers Ask This
Interviewers at Netflix ask this to evaluate your ability to balance time complexity constraints with memory efficiency. They specifically test if you understand that standard heaps offer O(log n) operations, and whether you can engineer a hybrid approach using a hash map to achieve the requested O(1) lookups for current prices while maintaining heap properties for min/max retrieval.
How to Answer This Question
1. Clarify Requirements: Confirm if duplicate stock prices are allowed and how to handle stale entries when a price updates.
2. Propose the Hybrid Architecture: Suggest using two heaps (min-heap and max-heap) for ordering, paired with a hash map storing the latest valid price for each symbol.
3. Define Lazy Deletion: Explain that you will not physically remove old values from heaps immediately; instead, mark them as invalid in the hash map or track their status.
4. Walk Through Operations: Detail how 'record' updates the map and pushes to both heaps, while 'getMax' and 'getMin' pop invalid top elements until a valid one is found.
5. Analyze Complexity: Conclude by proving O(1) for current price via the map and amortized O(1) for min/max retrieval after cleaning the heap tops, acknowledging the space trade-off.
Key Points to Cover
- Explicitly proposing a Hash Map for O(1) current price access
- Explaining the concept of Lazy Deletion for stale heap entries
- Clarifying that duplicate prices require unique identifiers like timestamps
- Demonstrating awareness of amortized vs. worst-case time complexity
- Connecting the solution to real-world scalability needs
Sample Answer
To solve this efficiently, I would design a system combining two priority queues with a hash map. First, I'd maintain a hash map where keys are stock symbols and values are their most recent prices. This ensures O(1) access to the current price. For the minimum and maximum queries, I would use a min-heap and a max-heap respectively, storing tuples of (price, timestamp, symbol). When recording a new price, I update the hash map and push the new entry into both heaps. Crucially, I cannot remove old entries from the heaps instantly without violating O(log n) constraints. Instead, I implement lazy deletion. When retrieving the maximum, I peek at the max-heap root. If the price there doesn't match the current value in my hash map, it's stale data, so I pop it and repeat until I find a valid entry. The same logic applies to the minimum. While individual operations might occasionally take longer due to cleanup, the amortized time complexity remains efficient. This approach mirrors Netflix's need for scalable real-time data processing, ensuring we get instant price checks while keeping order statistics robust without excessive memory churn from constant deletions.
Common Mistakes to Avoid
- Attempting to remove elements from the middle of a heap which is an O(n) operation
- Ignoring the handling of duplicate prices or stale data updates
- Claiming O(1) for heap operations without explaining the lazy removal strategy
- Failing to define how the hash map stays synchronized with heap updates
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.
Related Interview Questions
Find K Closest Elements (Heaps)
Medium
MetaEvaluate Reverse Polish Notation (Stack)
Medium
SpotifyConvert Binary Tree to Doubly Linked List in Place
Hard
MicrosoftDesign a Set with $O(1)$ `insert`, `remove`, and `check`
Easy
CiscoShould Netflix launch a free, ad-supported tier?
Hard
NetflixWhat Do You Dislike in a Project
Easy
Netflix