~/problems / Simulation & OOP design / Object-oriented design and extensible simulations

OA: Object Field Offset Manager

medium 3 levels ~45 min Jane Street

Level 1 Where does each field live?

A serializer lays out a record as a run of bytes: its fields are stored one after another in declaration order, the first at byte 0, each field taking a fixed number of bytes and the next one starting right where it ends (no gaps).

Build ObjectLayout(fields) where fields is a list of (name, size) pairs, and implement:

  • offset(name) -> int: the byte where the field starts.
  • size(name) -> int: its size in bytes.
  • total_size() -> int: the size of the whole record (0 with no fields).
  • field_at(byte) -> str | None: the name of the field that covers that byte, or None if byte is outside [0, total_size()).

Errors: a duplicate name or a size below 1 raises ValueError in the constructor; asking offset/size for an unknown name raises KeyError.

layout = ObjectLayout([("tag", 2), ("price", 8), ("qty", 4)])
layout.offset("price")   # 2
layout.offset("qty")     # 10
layout.total_size()      # 14
layout.field_at(9)       # "price"   (bytes 2..9)
layout.field_at(10)      # "qty"
layout.field_at(14)      # None
layout.offset("side")    # KeyError

A record may have 10**5 fields and the tests ask 10**5 questions, so don't walk the field list on every call.

Show hint

Precompute a list of start offsets in the constructor plus a name -> index dict. field_at is then a bisect_right(starts, byte) - 1.

Level 2 unlocks when level 1 passes.

Level 3 unlocks when level 2 passes.

Topic: Object-oriented design and extensible simulations. Classes that survive new requirements: games, payments, subscriptions, refactors.

0:00
Ctrl ' run · Ctrl ↵ submit
esc