Pure StorageEvent Fire / Callback Registration (Trigger System)
Implement an event that callers register callbacks on: callbacks registered before fire() are queued and run when the event fires; callbacks registered after it has fired run immediately. Then make it thread-safe. The follow-ups (register racing with fire, never invoking callbacks under the lock, where the fired flag is set relative to the critical section) decide the outcome, and the expected final version varies by interviewer.
Problem
Implement an event object (often framed as a "trigger system") with two operations:
register_callback(cb)— if the event has not fired yet, storecb; if it has already fired, invokecbimmediately.fire()— mark the event as fired and invoke every callback registered so far.
Each callback must run exactly once. Start with a single-threaded version, then make it safe when register_callback and fire are called concurrently from different threads.
Variants
Different interviewers want different end states for the thread-safe version:
- Lock, but never call out under the lock. Guard the queue and the
firedflag with a mutex, and release it around each callback invocation so user code never runs while the lock is held. - Set
firedfirst, then drain.fire()sets the flag under the lock, releases it, then drains the queue. A callback registered after the flag flips runs immediately, so callbacks may run out of registration order, but every callback still runs. - Drain until empty, then set
fired.fire()loops: under the lock, pop one callback if the queue is non-empty, release the lock to run it, then re-acquire; once it is empty, setfired = truewhile still holding the lock. There is no window in which a registration seesfired == falseafter the drain has finished. - No-lock version. Some interviewers push for an attempt without a mutex (re-check
firedafter enqueueing and drain whatever is left). Be ready to explain exactly which interleavings it still gets wrong.
Pitfalls
- A candidate failed a screen over this Python follow-up and said it was fine:
The flag write is outside the critical section, so a concurrent
self.lock.acquire() self.lock.release() self.flag = Trueregister_callbackcan read the old value, enqueue its callback after the drain, and that callback never runs. Every read and write of the shared state has to happen while the lock is held. - Invoking callbacks while holding the mutex is a deadlock risk. Release the lock before calling out.
- The check-then-enqueue in
register_callbackmust be atomic with respect tofire(). Checkingfiredand pushing to the queue in two separate critical sections reopens the race. - The base question is well known, so the follow-ups decide the outcome, and they differ a lot between interviewers. Candidates who memorized one version still got caught by a new twist.
- Senior screens go deep on multithreading. A clean single-threaded version is only the starting point.
Follow-ups
Preparation
- Write all four variants above in your interview language and be able to explain the trade-off between each pair.
- For each version, draw out the "register checks the flag ↔ fire sets the flag" interleaving step by step and confirm no callback is lost or run twice.
- Practice spotting lock misuse quickly (writes outside the critical section, early returns that skip
release, calling out under a lock). Interviewers show you snippets and ask whether they are correct.
