Stock Price Fluctuation - Problem
Stock Price Fluctuation Tracker
You're building a real-time stock monitoring system that handles chaotic market data. In the fast-paced world of trading, price updates arrive out of order and sometimes contain errors that need correction.
Your system receives a stream of records, each containing a
• Records don't arrive chronologically
• Multiple records may have the same timestamp (corrections)
• Later records with the same timestamp override earlier ones
Design a StockPrice class that supports:
•
•
•
•
Example: If you receive prices at timestamps [3,1,2,3] with values [100,60,80,110], the final state should show timestamp 3 has price 110 (corrected), and current() returns 110 since timestamp 3 is latest.
You're building a real-time stock monitoring system that handles chaotic market data. In the fast-paced world of trading, price updates arrive out of order and sometimes contain errors that need correction.
Your system receives a stream of records, each containing a
timestamp and corresponding stock price. However:• Records don't arrive chronologically
• Multiple records may have the same timestamp (corrections)
• Later records with the same timestamp override earlier ones
Design a StockPrice class that supports:
•
update(timestamp, price) - Update/correct price at given timestamp•
current() - Get the most recent price (latest timestamp)•
maximum() - Get the highest price across all current records•
minimum() - Get the lowest price across all current recordsExample: If you receive prices at timestamps [3,1,2,3] with values [100,60,80,110], the final state should show timestamp 3 has price 110 (corrected), and current() returns 110 since timestamp 3 is latest.
Input & Output
example_1.py — Basic Operations
$
Input:
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
›
Output:
[null, null, null, 5, 10, null, 5, null, 2]
💡 Note:
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with prices [10].
stockPrice.update(2, 5); // Timestamps are [1,2] with prices [10,5].
stockPrice.current(); // return 5, the latest timestamp is 2 with price 5.
stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, corrected to 3.
stockPrice.maximum(); // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2); // Timestamps are [1,2,4] with prices [3,5,2].
stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4.
example_2.py — Price Corrections
$
Input:
["StockPrice", "update", "update", "update", "maximum", "update", "maximum", "minimum"]
[[], [1, 100], [2, 200], [3, 50], [], [2, 300], [], []]
›
Output:
[null, null, null, null, 200, null, 300, 50]
💡 Note:
Multiple price updates including correction at timestamp 2 from 200 to 300. The maximum changes from 200 to 300, while minimum remains 50.
example_3.py — Edge Case: Single Update
$
Input:
["StockPrice", "update", "current", "maximum", "minimum"]
[[], [1, 42], [], [], []]
›
Output:
[null, null, 42, 42, 42]
💡 Note:
With only one price record, current, maximum, and minimum all return the same value.
Constraints
- 1 ≤ timestamp, price ≤ 109
- At most 105 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
Visualization
Tap to expand
Understanding the Visualization
1
Chaotic Updates
Price updates arrive out of order and corrections overwrite previous values
2
Hash Map Storage
Use hash map for O(1) price lookups and updates, tracking latest timestamp
3
Heap Management
Maintain max and min heaps for efficient extreme value queries
4
Lazy Validation
When querying heaps, validate top entries against current hash map values
Key Takeaway
🎯 Key Insight: Using heaps with lazy deletion allows us to handle price corrections efficiently - we validate heap entries against the hash map only when needed, achieving optimal time complexity for all operations.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code