KatInterview

Build an In-Memory Price History Store with One-Shot Updates

Data Structure DesignModel answer
MediumModel answerData Structure Designdata-structure-designtime-seriesatomic-updates

Last updated

Design a per-symbol price store supporting lookups, custom change formulas, atomic range updates that can touch each slot only once, and a switch from daily to hourly data.

You are given a batch of historical price records for traded securities. Every record has:

  • symbol: a ticker string
  • timestamp: at first a calendar date (one price per day)
  • price: a decimal number

Build an in-memory class, PriceHistoryStore, built from those records. You choose the exact method signatures and return types, but the behavior must be well defined and easy to test.

Required operations

  1. Point lookup. get_price(symbol, date) returns the price of that symbol on that date. If no price exists, return a clear "missing" result (for example None).
  2. Change between two dates. get_change(symbol, start_date, end_date) returns how the price moved between the two dates. The calculation is a pluggable function such as formula(start_price, end_price), supplied by the caller. Decide what happens if either price is missing or the formula cannot be evaluated (for example, a percentage change from a start price of zero).
  3. One-shot range update. update_prices(symbol, start_date, end_date, new_prices) overwrites the prices of one symbol across the inclusive date range and returns whether it succeeded.
    • If any slot in that range has been changed by an earlier successful update, the whole call fails.
    • The call is atomic: either every price is written or none is.
  4. Hourly data. The granularity is now one price per hour instead of per day. Add get_hourly_prices(symbol, day), which returns every hourly price for that symbol on that calendar day, ordered by time.

What to cover

State and handle the following explicitly:

  • duplicate initial records (identical and conflicting)
  • unknown symbols and missing dates
  • invalid ranges (start after end, or the wrong number of new prices)
  • repeated and partially overlapping updates
  • failures that would otherwise leave a half-applied update
  • parsing of dates and times (strings, date objects, misaligned or timezone-aware values)

Finish with a set of representative test cases for your implementation.

Hints

Hint 1

Keep a separate structure per symbol: a hash map from timestamp to price for O(1) lookups, plus a sorted list of timestamps so a whole day can be sliced out later.

Hint 2

For atomicity, use validate-then-commit: parse every input and run every check that can fail (range, length, overlap with already-updated slots) before writing anything. The write phase then cannot fail midway.

Hint 3

Normalize at the boundary (symbol case and whitespace, timestamps onto the granularity grid, prices as Decimal), so daily and hourly modes differ only in the step size. For hourly queries, two binary searches (day start and next day start) give the slice.