Stock Price Fluctuation
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
A feed delivers stock price records as (timestamp, price) pairs. Records can arrive out of order, and a later record for a timestamp that was already seen is a **correction** that replaces the earlier price for that timestamp.
Implement StockPrice:
- StockPrice() starts with no records.
- update(timestamp, price) sets the price at timestamp to price (inserting or correcting).
- current() returns the price at the **largest** timestamp seen so far.
- maximum() returns the highest price among the current (corrected) records.
- minimum() returns the lowest price among the current (corrected) records.
current, maximum and minimum are only called after at least one update. Aim for O(log n) amortized per operation.
Examples
Constraints
- 1 <= timestamp, price <= 10^9
- At most 10^5 calls are made in total.
- `current`, `maximum` and `minimum` are called only after at least one `update`.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.