Practice/Snapchat/Leetcode 2034. Stock Price Fluctuation
CodingMust
You are building a real-time stock price tracking system that receives price updates throughout the trading day. The system must handle several challenges:
Price updates can arrive out of chronological order (e.g., you might receive the price at timestamp 5 before receiving the price at timestamp 3)
Price corrections can occur where a previously recorded price at a given timestamp needs to be updated with a new value
The system must efficiently support queries for:
Design and implement a StockTracker class that supports these operations efficiently.
update(timestamp, price) which records or updates the stock price at the given timestampcurrent() which returns the price at the latest (highest) timestampmaximum() which returns the highest price across all timestampsminimum() which returns the lowest price across all timestampsExample 1:
Operations: tracker = StockTracker() tracker.update(1, 100) // Record price 100 at timestamp 1 tracker.update(2, 150) // Record price 150 at timestamp 2 tracker.current() // Returns 150 (price at latest timestamp 2) tracker.maximum() // Returns 150 (highest price overall) tracker.minimum() // Returns 100 (lowest price overall)
Example 2:
Operations: tracker = StockTracker() tracker.update(5, 200) // Record price 200 at timestamp 5 tracker.update(1, 100) // Record price 100 at timestamp 1 (out of order) tracker.update(3, 150) // Record price 150 at timestamp 3 tracker.current() // Returns 200 (timestamp 5 is still latest) tracker.maximum() // Returns 200 tracker.minimum() // Returns 100
Example 3:
Operations: tracker = StockTracker() tracker.update(1, 100) // Record price 100 at timestamp 1 tracker.update(2, 200) // Record price 200 at timestamp 2 tracker.update(1, 120) // Correct timestamp 1 to price 120 tracker.current() // Returns 200 (latest timestamp 2) tracker.maximum() // Returns 200 tracker.minimum() // Returns 120 (old price 100 removed)