KatInterview

Simulate Message Spreading in an Orbital Network

Data Structure DesignModel answer
MediumModel answerData Structure Designtrees-graphsheaps-sortingdesign-ds

Last updated

Simulate timed, discrete-event message propagation across a satellite graph and fire report callbacks in exact time and ID order.

Design an online simulation class SatelliteNetwork that models message propagation and reporting over an undirected network of satellites.

Implement the following public operations:

  1. satellite_connected(satellite_id): Adds a new satellite node. If satellite_id was already registered, trigger the callback ErrDuplicateSatelliteId(satellite_id) and do not modify the network.
  2. relationship_established(satellite_id1, satellite_id2): Establishes a bidirectional link between two registered satellites. If either ID is not registered, invoke ErrInvalidSatelliteId(offending_id) for the first invalid ID encountered and ignore the operation.
  3. message_received(satellite_ids): Simulates an independent broadcast starting simultaneously at time $t = 0$ at all listed satellites. If any ID in satellite_ids is invalid, invoke ErrInvalidSatelliteId(offending_id) for each invalid reference.

Propagation Mechanics

  • Transmission Speed: When a satellite receives a message at time $t$, it attempts to forward it to all directly connected neighbors in increasing SatelliteId order. Each individual forwarding attempt takes exactly 10 seconds of sender time. The first attempt reaches its target at $t + 10$, the second at $t + 20$, and so forth.
  • Parent Exemption: A satellite never forwards the message back to the sender that first notified it.
  • First Arrival Wins: A satellite processes only its first message reception. Receptions occurring later are ignored.
  • In-Flight Delays: If neighbor $V$ receives the message from another route while sender $U$ is transmitting to $V$, sender $U$'s 10-second transmission attempt is not cancelled; $U$ remains busy for those 10 seconds before moving to its next candidate neighbor.
  • Neighbor Skipping: At the instant a satellite is free to start its next transmission, it skips any neighbor that has already received the message (or is its parent) at zero cost in time.

Reporting Back to Earth

  • A satellite becomes ready to report back to Earth as soon as all of its direct neighbors have received the message (from any source).
  • Upon satisfying this condition at time $T_{\text{all_notified}}$, the satellite spends 30 seconds preparing its report and sends it back at $T_{\text{report}} = T_{\text{all_notified}} + 30$.
  • Note that $T_{\text{all_notified}}$ is equivalent to $\max(\text{recv}[u], \max_{v \in \text{neighbors}(u)} \text{recv}[v])$. For an isolated satellite with no neighbors, $T_{\text{all_notified}} = \text{recv}[u]$.
  • Whenever a report arrives, trigger the callback OnSatelliteReportedBack(satellite_id).
  • Callbacks for a message run must be triggered in strictly non-decreasing order of delivery time. If multiple reports complete at the exact same second, invoke them in ascending order of satellite_id.

Hints

Hint 1

Model the network propagation as a discrete-event simulation using a min-heap ordered by timestamp.

Hint 2

When multiple events occur at the exact same timestamp t, process message deliveries (DELIVER) before satellite state updates (ADVANCE) to ensure senders see up-to-date recipient statuses at time t.

Hint 3

After computing the first arrival time recv[u] for every reachable satellite, the report time for any satellite u simplifies to max(recv[u], max_{v in neighbors(u)} recv[v]) + 30.