Many timers on top of one underlying timer
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's HackerRank assessment, last asked Dec 2025. It tests heaps and data structure design.
The system gives you one hardware-style timer: set_timer(timestamp) schedules it for an absolute time, and calling it again replaces the previous setting. When it expires, the system calls your handle_timer(). Build a class that lets users schedule many independent timers on top of it, and fires on_expire once for every user timer at the right time.
Interface
Unless you agree on a different interface with the interviewer, assume:
- Given:
set_timer(ts: int) -> None(one pending alarm only; a new call overrides),now() -> int, and the callbackhandle_timer()that the system invokes when the alarm fires. -
set_new_timer(ts: int, callback) -> int— schedule a user timer and return its id. Ifts <= now(), it fires as soon as possible. -
cancel(timer_id: int) -> bool— cancel a pending timer;Falseif it already fired or never existed. -
handle_timer() -> None— fire every user timer whose time has come, then arm the system timer for the next one.
Example
now() = 100
set_new_timer(150, a) -> 1 # set_timer(150)
set_new_timer(120, b) -> 2 # earlier deadline: set_timer(120)
set_new_timer(150, c) -> 3 # same as an existing deadline: no re-arm needed
cancel(1) -> True
# at 120: handle_timer() runs b, then set_timer(150)
# at 150: handle_timer() runs c (1 was cancelled), nothing left, no re-arm
Constraints
- Timestamps are absolute and comparable with
now(). Several timers may share a timestamp. - The system timer may fire slightly late; never fire a user timer early.
Clarifying questions
- Do callbacks run on the timer thread, and can they schedule new timers?
- Should cancel be supported, and must it be O(log n)?
- How should a timer in the past behave?
- Is the class used from several threads at once?
What a strong answer covers
- A min-heap of
(ts, seq, id); re-arm the system timer only when the earliest deadline changes (an insert that becomes the new minimum, or after firing). -
handle_timerpops and fires everything withts <= now()in a loop, because the alarm can be late and several timers can share a time; then arms for the new minimum, if any. - Cancel by lazy deletion (a cancelled set or a flag), skipping cancelled entries when they reach the top; re-arm if the cancelled one was the earliest, or let a spurious wake-up do nothing.
- Ties ordered by insertion, and callbacks that add timers while firing.
- Thread safety: one lock around the heap, and callbacks invoked outside it to avoid deadlocks.
- O(log n) per add and cancel, O(k log n) to fire
ktimers.
Follow-ups
- Millions of timers with coarse precision: a hierarchical timing wheel.
- Repeating timers.
- The clock can jump backwards (NTP). What breaks, and should you use a monotonic clock?
Where it's asked
- Google · HackerRank assessment · Last asked Dec 202590 min · Two problems on HackerRank · Some early-career assessments enable the built-in assistant; others forbid AI. The invitation says which.
What it tests
heaps · data structure design
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
