Track simulations and report timeouts
Problem Description
You are tasked with implementing a monitoring component designed to track numerous concurrent simulations. This component will process a continuous stream of events, identify simulations that have timed out, and report them.
Event Structure and Semantics
Each simulation produces event objects with the following structure:
sim_id(string or integer): A unique identifier for the simulation.type(string): Indicates the event type, one of"start","ping", or"end".timestamp(integer or long): The event time in seconds or milliseconds, guaranteed to be monotonically non-decreasing across all events.
The semantics for each event type are:
start: The simulationsim_idhas begun at thistimestamp. Its last activity timestamp is set to thistimestamp.ping: The simulationsim_idis still active. Its last activity timestamp is updated to thistimestamp.end: The simulationsim_idhas completed normally at thistimestamp. It should no longer be considered active.
Timeout Condition
You are provided with a timeout limit T (e.g., 3 seconds). A simulation is considered timed out and automatically ended if its inactivity duration exceeds T. Specifically, a simulation sim_id times out if:
current_time - last_activity_timestamp(sim_id) > T
where current_time is the timestamp of the most recently processed event.
processEvent Function
Design a function, processEvent(event), that receives events one by one in chronological order. Upon receiving a new event with timestamp ts_current, it must perform the following actions:
- Update State: Adjust the internal state based on the incoming event (
start,ping, orend). - Identify Timeouts: Determine which simulations have just become timed out at or before
ts_current. These are simulations that:- Are currently active (not previously ended by an
endevent or a prior timeout). - Satisfy the timeout condition:
ts_current - last_activity_timestamp > T.
- Are currently active (not previously ended by an
- Mark as Ended: Mark these newly timed-out simulations as ended so they are not reported again.
- Return Timed-Out IDs: Return a set or list of all
sim_ids that have just been identified as timed out.
Performance and Implementation Constraints
- The function must be efficient for a large number of concurrent simulations and events.
- Aim for a time complexity close to O(log n) per event, where
nis the number of active simulations. - You are required to specify the data structures you would use and describe the key operations your implementation must support. Full code is not necessary.
- Assume the process is long-running and must handle many events over an extended period without requiring restarts.
AI interview prep
Core dilemma
Efficiently tracking and querying for dynamically expiring entities in a large, constantly changing set of active simulations, while strictly adhering to an O(log N) per-event processing time constraint.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Track simulations and report timeouts
Problem Description
You are tasked with implementing a monitoring component designed to track numerous concurrent simulations. This component will process a continuous stream of events, identify simulations that have timed out, and report them.
Event Structure and Semantics
Each simulation produces event objects with the following structure:
sim_id(string or integer): A unique identifier for the simulation.type(string): Indicates the event type, one of"start","ping", or"end".timestamp(integer or long): The event time in seconds or milliseconds, guaranteed to be monotonically non-decreasing across all events.
The semantics for each event type are:
start: The simulationsim_idhas begun at thistimestamp. Its last activity timestamp is set to thistimestamp.ping: The simulationsim_idis still active. Its last activity timestamp is updated to thistimestamp.end: The simulationsim_idhas completed normally at thistimestamp. It should no longer be considered active.
Timeout Condition
You are provided with a timeout limit T (e.g., 3 seconds). A simulation is considered timed out and automatically ended if its inactivity duration exceeds T. Specifically, a simulation sim_id times out if:
current_time - last_activity_timestamp(sim_id) > T
where current_time is the timestamp of the most recently processed event.
processEvent Function
Design a function, processEvent(event), that receives events one by one in chronological order. Upon receiving a new event with timestamp ts_current, it must perform the following actions:
- Update State: Adjust the internal state based on the incoming event (
start,ping, orend). - Identify Timeouts: Determine which simulations have just become timed out at or before
ts_current. These are simulations that:- Are currently active (not previously ended by an
endevent or a prior timeout). - Satisfy the timeout condition:
ts_current - last_activity_timestamp > T.
- Are currently active (not previously ended by an
- Mark as Ended: Mark these newly timed-out simulations as ended so they are not reported again.
- Return Timed-Out IDs: Return a set or list of all
sim_ids that have just been identified as timed out.
Performance and Implementation Constraints
- The function must be efficient for a large number of concurrent simulations and events.
- Aim for a time complexity close to O(log n) per event, where
nis the number of active simulations. - You are required to specify the data structures you would use and describe the key operations your implementation must support. Full code is not necessary.
- Assume the process is long-running and must handle many events over an extended period without requiring restarts.