Katkat

Design a satellite propagation simulator

OptiverSoftware Engineer · Online Assessment

Last updated

Optiver software-engineer take-home: implement a SatelliteNetwork that simulates timed message propagation over an undirected graph and fires OnSatelliteReportedBack callbacks in deterministic order. It tests graph modeling, discrete-event simulation, priority-queue scheduling, and careful handling of 10s-per-hop forwarding and 30s report timing with tie-breaking by SatelliteId.

Question

Implement a SatelliteNetwork class that simulates message propagation over an undirected satellite graph. The class consumes a stream of instructions online and triggers callbacks as the simulation unfolds. Implement the following methods:

  1. satellite_connected(satellite_id) — Register a new satellite. If satellite_id already exists, call ErrDuplicateSatelliteId(satellite_id) and do not add it again.

  2. relationship_established(satellite_id1, satellite_id2) — Create a bidirectional (undirected) link between the two satellites. If either ID does not exist, skip the instruction and call ErrInvalidSatelliteId(offending_id) for the invalid ID.

  3. message_received(satellite_ids) — Earth sends the same message simultaneously at time t = 0 to all listed satellites. If any listed ID does not exist, call ErrInvalidSatelliteId(offending_id). Treat each call to message_received as an independent run (reset all per-message state between runs).

Propagation rules

  • When a satellite first receives the message at time t, it attempts to forward it to each directly connected neighbor that has not yet received it. Forwarding is done one neighbor at a time, in increasing SatelliteId order, and each send takes exactly 10 seconds (so the first neighbor is reached at t + 10, the second at t + 20, and so on).
  • A satellite never attempts to notify the satellite that notified it.
  • Different satellites may concurrently attempt to notify the same neighbor; each attempt still consumes 10 seconds, but as soon as any attempt completes, that neighbor is considered notified and no further attempts to it should proceed (the neighbor takes its earliest arrival time).
  • A satellite ignores all receptions after its first.

Reporting back to Earth

  • A satellite becomes eligible to report back once all of its direct neighbors have received the message (from any sender). At the earliest time this condition becomes true, the satellite spends 30 seconds processing and then reports back — i.e. its report is delivered at (time the last neighbor was notified) + 30.
  • For each report, call OnSatelliteReportedBack(satellite_id) in exact chronological order for that message.
  • When multiple reports are delivered at the same time, invoke the callbacks in increasing SatelliteId order.
  • Do not return or print anything else.

Assumptions / constraints

  • Satellite IDs are integers.
  • There may be multiple calls to message_received; each is an independent run.
  • Handle duplicate and invalid references exactly as specified via the error callbacks.
  • Design your data structures and algorithm to handle large N satellites and M links efficiently, while guaranteeing deterministic output ordering.

Provide code or pseudocode and analyze the time and space complexity.