Medium · System Design

Stock Price Fluctuation

Given a stream of update(timestamp, price) records in which a repeated timestamp corrects its earlier price, implement current() — the price at the latest timestamp — and maximum() and minimum() — the highest and lowest current prices over all timestamps.

Examples

Example 1

{
  "ops": [
    {"kind": "update", "timestamp": 1, "price": 10},
    {"kind": "update", "timestamp": 2, "price": 5},
    {"kind": "maximum"},
    {"kind": "minimum"},
    {"kind": "current"}
  ]
}

Output: [10, 5, 5]

Example 2

{
  "ops": [
    {"kind": "update", "timestamp": 1, "price": 10},
    {"kind": "update", "timestamp": 2, "price": 5},
    {"kind": "update", "timestamp": 1, "price": 3},
    {"kind": "current"},
    {"kind": "maximum"},
    {"kind": "minimum"}
  ]
}

Output: { "current": 5, "max": 5, "min": 3 }

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems