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 due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.

Design an algorithm that:

  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.

Implement the StockPrice class:

  • StockPrice() Initializes the object with no price records.
  • void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
  • int current() Returns the latest price of the stock.
  • int maximum() Returns the maximum price of the stock.
  • int minimum() Returns the minimum price of the stock.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"] values = [[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
Output: [10, 10, 5, 2]
💡 Note: Update t=1→$10, t=2→$5. Current (latest) is $5. Max is $10. Update t=1→$3 (correction). Max is now $5. Update t=4→$2. Min is $2.
Example 2 — Price Corrections
$ Input: operations = ["StockPrice", "update", "update", "maximum", "update", "maximum", "current"] values = [[], [1, 100], [2, 200], [], [1, 50], [], []]
Output: [200, 200, 200]
💡 Note: Update t=1→$100, t=2→$200. Max is $200. Correct t=1→$50. Max still $200. Current (t=2) is $200.
Example 3 — Single Timestamp Updates
$ Input: operations = ["StockPrice", "update", "current", "maximum", "minimum"] values = [[], [1, 50], [], [], []]
Output: [50, 50, 50]
💡 Note: Only one timestamp. Current, maximum, and minimum are all $50.

Constraints

  • 1 ≤ timestamp, price ≤ 109
  • At most 105 calls will be made to update, current, maximum, and minimum.
  • current, maximum, and minimum will be called only after update has been called at least once.

Visualization

Tap to expand
Stock Price Fluctuation: Real-time Tracking SystemUpdatest=1, $10t=2, $5Hash Mapt=1→$10t=2→$5 (latest)Min Heap$5minimumMax Heap$10maximumQueriescurrent(): $5max(): $10Handles out-of-order updates and price corrections efficientlyUpdate: O(log n) | Current: O(1) | Max/Min: O(log n)
Understanding the Visualization
1
Input Stream
Receive timestamp-price updates (possibly out-of-order)
2
Data Structure
Hash map + heaps track current/max/min efficiently
3
Queries
Return current, maximum, or minimum prices
Key Takeaway
🎯 Key Insight: Combine hash map for fast timestamp lookup with heaps for efficient min/max tracking
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 25
89.0K Views
High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen