Track the latest, maximum and minimum stock price when records arrive out of order and get corrected
Company: Segment
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
You are given a stream of records about a particular stock. Each record contains a `timestamp` and the corresponding `price` of the stock at that timestamp.
Unfortunately, the records do not arrive in chronological order, and some records may be incorrect. A later record with the same `timestamp` corrects the price of the previous wrong record at that timestamp.
Design an algorithm that supports updating the price at a timestamp, querying the latest price (by largest timestamp seen), and querying the maximum and minimum prices ever recorded across all currently valid records.
Implement the following methods:
- `update(timestamp: int, price: int)`: update the price of the stock at the given timestamp, correcting any previous record at that timestamp.
- `current() → int`: return the latest price of the stock (the price at the largest timestamp seen so far).
- `maximum() → int`: return the maximum price the stock has been across all currently valid records.
- `minimum() → int`: return the minimum price the stock has been across all currently valid records.
Example 1:
```text
Input:
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "current", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], [], []]
Output: [null, null, null, 5, 10, null, 5, null, 2, 2]
Explanation:
stock = StockPrice() # empty stream
stock.update(1, 10) # timestamps: {1=10}, latest=1
stock.update(2, 5) # timestamps: {1=10, 2=5}, latest=2
stock.current() # 5 (price at latest timestamp 2)
stock.maximum() # 10 (max across {10, 5})
stock.update(1, 3) # corrects ts=1 from 10 to 3, timestamps: {1=3, 2=5}
stock.maximum() # 5 (10 was stale, max across {3, 5})
stock.update(4, 2) # timestamps: {1=3, 2=5, 4=2}, latest=4
stock.current() # 2 (price at latest timestamp 4)
stock.minimum() # 2 (min across {3, 5, 2})
```
Example 2:
```text
Input:
["StockPrice", "update", "update", "current", "maximum", "minimum", "update", "current", "maximum", "minimum"]
[[], [2, 100], [1, 80], [], [], [], [2, 70], [], [], []]
Output: [null, null, null, 100, 100, 80, null, 70, 80, 70]
Explanation:
stock = StockPrice() # empty stream
stock.update(2, 100) # timestamps: {2=100}, latest=2
stock.update(1, 80) # timestamps: {1=80, 2=100}, latest=2
stock.current() # 100 (price at latest timestamp 2)
stock.maximum() # 100 (max across {80, 100})
stock.minimum() # 80 (min across {80, 100})
stock.update(2, 70) # corrects ts=2 from 100 to 70, timestamps: {1=80, 2=70}
stock.current() # 70 (price at latest timestamp 2)
stock.maximum() # 80 (100 was stale, max across {80, 70})
stock.minimum() # 70 (min across {80, 70})
```
```hint Stale values
A correction makes an old price invalid, and most heap implementations cannot delete an arbitrary element cheaply. Think about how a query could recognize an outdated entry when it reaches one.
```
```hint The latest price
The latest price belongs to the largest timestamp seen, which may not be the last record to arrive, and that timestamp's price can itself be corrected later.
```
### Constraints & Assumptions
- `1 <= timestamp, price <= 10^9`
- At most `10^5` calls will be made in total to `update`, `current`, `maximum` and `minimum`.
- `current`, `maximum` and `minimum` will be called only after `update` has been called at least once.
### What a Strong Answer Covers
- Storing the current price per timestamp so a correction replaces the old value, and tracking the largest timestamp independently of arrival order.
- A structure that answers maximum and minimum after corrections, with an argument for why a stale price is never returned.
- Time and space complexity per method, including the amortized cost of any cleanup and the memory held by outdated entries.
- Edge cases such as out-of-order arrivals, a correction at the latest timestamp, and repeated updates with the same price.
### Follow-up Questions
- How would you bound memory when corrections far outnumber queries?
- How would you support deleting the record at a timestamp, including the latest one?
- How would you answer the maximum and minimum over a timestamp range `[t1, t2]`?
- What changes if the stream covers many stock symbols and updates arrive concurrently?
Overview: Design a class that ingests out-of-order stock price records, where a repeated timestamp corrects an earlier price, and answers latest, maximum and minimum price queries. It tests choosing data structures that stay correct when old values become invalid, and amortized complexity analysis.