The Stock Price Fluctuation coding problem asks you to design a system that tracks the price of a stock over time. The system receives updates in the form of (timestamp, price). However, updates can arrive out of order, and a price for a previously recorded timestamp can be corrected. You need to provide the current price (at the latest timestamp), as well as the maximum, minimum, and latest prices recorded in the system at any given time.
This problem is a favorite at Microsoft, Meta, and Google because it tests your ability to choose and combine data structures for efficient updates and queries. It's a classic "system design in a single class" problem. You must handle high-frequency updates while maintaining sorted information (for min/max) and historical accuracy. It evaluates your understanding of the Data Stream and Ordered Set interview pattern.
To solve this efficiently, you need a combination of data structures.
timestamp -> price and a variable to track the maxTimestamp.current(), we return 110 because 10 is the largest timestamp.min() and max() on every query, which is .update() call always contains the current() price.When designing a class with multiple requirements (like latest, min, and max), don't try to find one data structure that does everything. Usually, the solution involves using a Hash Table for lookups and an Ordered Set or Heap for range queries.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Smallest Number in Infinite Set | Medium | Solve | |
| Design Task Manager | Medium | Solve | |
| Design a Number Container System | Medium | Solve | |
| Sequentially Ordinal Rank Tracker | Hard | Solve | |
| Design an Array Statistics Tracker | Hard | Solve |