← Questions

Report timed-out simulations from start, ping and end events

0
Applied IntuitionCodingLast asked Dec 2025

A coding interview question asked in Applied Intuition's technical phone screen, last asked Dec 2025. It tests event processing, heaps and hash map.

You monitor many simulations running at once. Each sends events: start when it begins, ping while it is alive, and end when it finishes. Events arrive one at a time in time order. A simulation that started, has not ended and has had no activity for longer than a timeout T is considered timed out and ended. After each event, return the simulations that have just timed out.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • Monitor(T: int)
  • process(sim_id: str, kind: str, ts: int) -> list[str]: applies the event, then returns every simulation that is still running and has ts - last_activity > T, ordered by last activity, then id. Those simulations are marked ended and never returned again.
  • start sets the last activity to ts; ping updates it; end ends the simulation. A ping or end for a simulation that is not running is ignored.

Example

m = Monitor(3)
m.process("a", "start", 0)   -> []
m.process("b", "start", 1)   -> []
m.process("b", "ping", 3)    -> []
m.process("c", "start", 4)   -> ["a"]     # 4 - 0 > 3
m.process("b", "end", 5)     -> []        # b ends normally
m.process("c", "ping", 6)    -> []
m.process("d", "start", 10)  -> ["c"]     # 10 - 6 > 3; b already ended

Constraints

  • Timestamps never decrease across events.
  • Millions of simulations over a long-running process; aim for O(log n) per event, amortized.

Clarifying questions

  • Is the timeout check strict (> T) or not (>= T)?
  • Is the event applied before the check? Can a ping that arrives late revive a simulation?
  • What about duplicate starts, or a start for a simulation that already ended or timed out?
  • Can events arrive out of order, and how late?
  • Should timeouts also be reported when no events arrive (a timer)?

What a strong answer covers

  • A hash map from simulation id to its state and last activity, plus a min-heap of (last_activity, id) with lazy deletion: on each event, pop entries older than ts - T, skipping stale ones whose time no longer matches.
  • Or an ordered structure (an OrderedDict used like an LRU list), since activity times only grow: move a simulation to the back on each ping and expire from the front in O(1).
  • Memory that stays bounded in a long-running process: drop ended simulations and stale heap entries.
  • Handling of edge cases: ping without start, duplicate ends, a timeout of zero.
  • Tests with interleaved simulations, ties in last activity, and an event that times out several at once.

Follow-ups

  • Add get_timed_out(now) that can be called without an event, from a timer.
  • Events can arrive out of order by up to a few seconds. How do you avoid false timeouts?
  • Run this as a service: what happens on restart, and how do you shard it across machines?

Where it's asked

What it tests

event processing · heaps · hash map

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding