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 - Heap-Based Optimization INPUT Stock Records Stream t=1, p=10 update t=2, p=5 update t=1, p=3 correction! t=4, p=2 update Operations: ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"] Values: [[], [1,10], [2,5], [], [], [1,3], [], [4,2], []] Data Structures: - HashMap for prices - Max Heap + Min Heap - Latest timestamp tracker ALGORITHM STEPS 1 Update Operation Store price in HashMap[ts] Push (price,ts) to both heaps 2 Current Price Track latest timestamp Return HashMap[latestTs] 3 Maximum Price Pop from maxHeap until top matches HashMap[ts] 4 Minimum Price Pop from minHeap until top matches HashMap[ts] Heaps (Lazy Deletion) MaxHeap (10,1) (5,2) (3,1) (2,4) MinHeap (2,4) (3,1) (5,2) (10,1) HashMap 1 --> 3 2 --> 5 4 --> 2 FINAL RESULT Query Results: current() Latest ts=2, price= 10 maximum() [first] Max before correction= 10 maximum() [second] After t=1 corrected= 5 minimum() Min price in stream= 2 Output Array: [10, 10, 5, 2] OK - All Queries Matched! Key Insight: Use lazy deletion with heaps: Instead of removing old prices when corrected, validate heap tops against the HashMap. If heap top's price doesn't match HashMap[timestamp], it's stale - pop and continue. Time: O(log n) amortized per operation | Space: O(n) for HashMap + heaps storing all updates. TutorialsPoint - Stock Price Fluctuation | Heap-Based Optimization Approach
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