JJobsMoi
Atlassian

Stock Price Fluctuation

Codingmedium

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 at timestamp the price was price, overwriting any price previously recorded for that same timestamp.
  • current() — returns the price at the latest timestamp seen so far (latest meaning the largest timestamp value recorded via update, 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, and minimum — stale prices should never be counted once they've been overwritten.
  • current() is based on the largest timestamp value ever given to update, regardless of call order.
  • It is guaranteed that current(), maximum(), and minimum() are only called after at least one update call 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) and update(4, 80) record two prices.
  • current() returns 80, the price at timestamp 4, the largest timestamp so far.
  • maximum() returns 80 (the max of 50 and 80).
  • update(3, 65) adds a third timestamp; maximum() is still 80.
  • update(4, 30) corrects the earlier price of 80 at timestamp 4 down to 30.
  • minimum() now returns 30, since the recorded prices are 50, 65, and 30.

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 5 holds price 40 (it was corrected from 100), and timestamp 1 holds 20.
  • current() returns 40 because timestamp 5 is 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, so current() still returns 40.

Constraints

  • 1 <= timestamp, price <= 10^9
  • At most 10^5 total calls will be made to update, current, maximum, and minimum combined.
  • current, maximum, and minimum are called only after update has been called at least once.

Solution

Loading editor…

Sign in to get AI feedback on your answer. Your work is saved while you do.

Sign in to evaluate