← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Jun 2026

Summary

Google SWE interview with a system design/coding hybrid question about managing multiple user timers on top of a single underlying timer API. Pretty tricky constraint-based problem that forced you to think carefully about data structures and edge cases rather than just slapping a heap on it and calling it done.

Questions Asked (1)

Q1

You have a Timer class with access to only one underlying system timer at a time. Implement setNewTimer(timestamp) so that multiple user timers can be outstanding simultaneously, with handleTimer() firing correctly for each expired timer. Discuss data structures, complexity, and edge cases like duplicate timestamps and overrides.

System DesignAlgorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was a min-heap keyed on timestamp, which is the right call, but I fumbled explaining why you need to reschedule the system timer every time setNewTimer is called with something earlier than the current head.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and then propose a min-heap of timers keyed by expiration time, with a single underlying system timer set to the earliest expiration. Explain how to handle duplicate timestamps and overrides, and analyze time complexity for insertion and firing.

Pro tip: Mention that using a min-heap allows O(log n) insertion and O(1) peek for the next timer, and that you can optimize duplicate timestamps by grouping them in a list at the same heap node to avoid redundant system timer resets.

1. Clarify requirements and constraints

Ask about the expected number of timers, whether timers can be cancelled, and the behavior for duplicate timestamps or overrides. Confirm that only one system timer can be active at a time.

2. Choose data structure

Propose a min-heap (priority queue) ordered by expiration timestamp, where each node stores the timestamp and a list of callbacks for that time. This efficiently retrieves the earliest timer.

3. Implement setNewTimer and handleTimer

In setNewTimer, insert the timer into the heap; if it becomes the new minimum, reset the system timer to its expiration. In handleTimer, pop all expired timers (timestamp <= current time), fire their callbacks, and set the system timer to the next earliest expiration.

4. Handle edge cases

Address duplicate timestamps by grouping them in the same heap node, overrides by allowing cancellation (e.g., lazy deletion with a cancelled flag), and empty heap by clearing the system timer.

5. Analyze complexity and trade-offs

Insertion is O(log n), firing expired timers is O(k log n) where k is the number fired, and peeking is O(1). Discuss alternative structures like balanced BST or timing wheel for different trade-offs.

Key Points to Mention

  • Min-heap (priority queue) for efficient retrieval of the earliest timer
  • Grouping duplicate timestamps in a list to avoid redundant system timer resets
  • Lazy deletion or cancellation flag for overridden timers
  • Time complexity: O(log n) insertion, O(1) peek, O(k log n) for firing k timers
  • Edge cases: empty heap, duplicate timestamps, overrides, timer cancellation
  • Alternative data structures like balanced BST or timing wheel and their trade-offs

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.