Stock Price Fluctuation
Problem
You are given a live stream of stock price updates arriving one at a time, and you must build a small system that keeps track of them and answers questions about the current state of the stock.
Each update is a pair (timestamp, price), meaning that at the given timestamp the stock traded at the given price. Updates for the same timestamp can arrive more than once, and a later update overwrites the price recorded for that timestamp (it does not create a second record). Updates do not necessarily arrive in increasing order of timestamp — an update for an earlier timestamp can show up after one for a later timestamp.
Design a class StockPrice that supports:
StockPrice()— initializes the object, with no price data recorded yet.update(timestamp, price)— records that attimestampthe price wasprice, overwriting any price previously recorded for that sametimestamp.current()— returns the price at the latest timestamp seen so far (latest meaning the largest timestamp value recorded viaupdate, not the order calls were made).maximum()— returns the highest price recorded across all timestamps seen so far, using the most up-to-date value for each timestamp.minimum()— returns the lowest price recorded across all timestamps seen so far, using the most up-to-date value for each timestamp.
Notes to keep in mind:
- A correction to an old timestamp must be reflected immediately in
current,maximum, andminimum— stale prices should never be counted once they've been overwritten. current()is based on the largest timestamp value ever given toupdate, regardless of call order.- It is guaranteed that
current(),maximum(), andminimum()are only called after at least oneupdatecall has happened.
Examples
Example 1:
Input:
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [2, 50], [4, 80], [], [], [3, 65], [], [4, 30], []]
Output:
[null, null, null, 80, 80, null, 80, null, 30]
Explanation:
update(2, 50)andupdate(4, 80)record two prices.current()returns80, the price at timestamp4, the largest timestamp so far.maximum()returns80(the max of50and80).update(3, 65)adds a third timestamp;maximum()is still80.update(4, 30)corrects the earlier price of80at timestamp4down to30.minimum()now returns30, since the recorded prices are50,65, and30.
Example 2:
Input:
["StockPrice", "update", "update", "update", "current", "update", "current"]
[[], [5, 100], [1, 20], [5, 40], [], [1, 90], []]
Output:
[null, null, null, null, 40, null, 40]
Explanation:
- After the first three updates, timestamp
5holds price40(it was corrected from100), and timestamp1holds20. current()returns40because timestamp5is still the largest timestamp recorded, even though it arrived out of order.update(1, 90)corrects an older timestamp, which does not change what the latest timestamp is, socurrent()still returns40.
Constraints
1 <= timestamp, price <= 10^9- At most
10^5total calls will be made toupdate,current,maximum, andminimumcombined. current,maximum, andminimumare called only afterupdatehas been called at least once.
Solution
Sign in to get AI feedback on your answer. Your work is saved while you do.
Sign in to evaluate