Design a Stock Price Fluctuation Tracker

Data Structures
Medium
Spotify
58.2K views

Design a data structure that allows you to record a new stock price and immediately retrieve the maximum, minimum, and current price in $O(1)$ time. Use two heaps and a single variable.

Why Interviewers Ask This

Interviewers at Spotify ask this to evaluate your ability to optimize for real-time data consistency under strict latency constraints. They specifically test if you can balance memory efficiency with speed, ensuring you understand that standard sorting is too slow for live financial feeds. This question probes your mastery of heap properties and lazy deletion techniques to solve the 'stale node' problem in dynamic datasets.

How to Answer This Question

1. Clarify Requirements: Confirm that updates are immediate and that retrieving max/min must be O(1). Ask if duplicate prices or invalid timestamps need handling. 2. Propose Core Structure: Suggest using a hash map to store current valid prices for O(1) lookups and two heaps (min-heap and max-heap) to track extremes. 3. Address Staleness: Explain the critical challenge where old values remain in heaps after an update. Detail how to use the hash map as the source of truth to lazily remove stale entries during retrieval. 4. Define Operations: Walk through inserting a price (update map, push to both heaps) and retrieving max/min (pop from heap until top matches current map value). 5. Analyze Complexity: Conclude by stating time complexity is amortized O(log N) for insertions but O(1) for retrieval, and space complexity is O(N) due to storing all historical updates in heaps.

Key Points to Cover

  • Identifying the 'stale node' problem inherent in using static heaps for dynamic data
  • Proposing a hybrid structure combining a Hash Map for truth and Heaps for ordering
  • Explaining the Lazy Deletion strategy to avoid expensive cleanup operations
  • Distinguishing between worst-case O(log N) insertion and amortized O(1) retrieval
  • Demonstrating awareness of memory overhead trade-offs in real-time systems

Sample Answer

To design this tracker efficiently, I would first clarify that we need to handle rapid updates where a stock price changes frequently. The core challenge is maintaining the maximum and minimum without re-scanning the entire dataset every time. My solution uses a combination of a hash map and two priority queues. I will maintain a hash map where the key is the timestamp or unique ID and the value is the current price. This acts as our single source of truth. For the heaps, I'll use a min-heap to track the lowest price and a max-heap for the highest. When a new price arrives, I update the hash map and push the new entry into both heaps. However, simply pushing isn't enough because old entries linger in the heaps. To handle this, I implement lazy deletion. When retrieving the maximum price, I check the top of the max-heap against the hash map. If the price in the heap doesn't match the current price in the map, it's stale, so I pop it and repeat. Once the tops match, I have the true maximum. The same logic applies to the minimum. While insertion takes O(log N), the retrieval is effectively O(1) on average because most checks pass immediately. This approach balances the need for instant access with the reality of changing data, which fits well with high-frequency trading environments.

Common Mistakes to Avoid

  • Attempting to rebuild the entire sorted list on every update, resulting in O(N) retrieval time
  • Forgetting to handle stale entries, leading to incorrect max/min values being returned
  • Using a single heap for both min and max, which forces inefficient conversions or full scans
  • Ignoring the memory cost of storing historical data in heaps when only current values matter

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.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 30 Spotify questions