← Google Interview Insights

Google·Software Engineer·Onsite - System Design / Architecture·Senior

SeniorPrefer not to say
Apr 2026

Summary

Google system design round focused entirely on building a Timer class from scratch. More depth than I expected for what seemed like a simple API question.

Questions Asked (1)

Q1

Design and implement a Timer class that supports scheduling a single timer (overwriting any existing one) and scheduling multiple concurrent timers. Walk through the data structures, thread-safety concerns, and how the two scheduling modes interact.

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

I went straight to a min-heap ordered by expiry time, which felt right, but then the interviewer started asking how setTimer interacts with already-running concurrent timers and I kind of fumbled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and constraints, then propose a design using a priority queue (min-heap) for efficient timer management, with a dedicated scheduler thread and proper synchronization. Discuss how single and multiple timer modes can coexist, and analyze trade-offs like precision, scalability, and resource usage.

Pro tip: Demonstrate awareness of real-world issues like timer drift, cancellation, and thread pool sizing; mentioning how you'd handle edge cases (e.g., timer firing during cancellation) shows depth beyond textbook solutions.

1. Clarify Requirements

Ask about expected timer precision, maximum number of concurrent timers, whether callbacks can be cancelled, and if the timer should be periodic or one-shot.

2. Design Data Structures

Propose a min-heap (priority queue) keyed by expiration time for efficient retrieval of the next timer, and a hash map for O(1) cancellation if needed.

3. Address Thread Safety

Use a mutex to protect shared data structures, and consider a condition variable to wake the scheduler thread when a new earlier timer is added or the earliest timer is cancelled.

4. Implement Scheduling Modes

For single timer mode, maintain a separate slot that overwrites any existing timer; for multiple timers, insert into the heap. Ensure both modes interact correctly by having the single timer also use the same heap but with a flag to cancel previous ones.

5. Discuss Trade-offs and Optimizations

Compare using a single scheduler thread vs. a thread pool, discuss precision vs. overhead, and mention alternatives like timing wheels for high-scale scenarios.

Key Points to Mention

  • Use of a min-heap (priority queue) for O(log n) insertion and O(1) peek of the next timer.
  • Thread safety via mutex and condition variable to avoid busy-waiting.
  • Handling of single timer mode by cancelling the previous timer (e.g., using a generation counter or flag).
  • Potential issues with timer drift and how to mitigate (e.g., using absolute time, adjusting for callback duration).
  • Scalability considerations: thread pool vs. single thread, and timing wheel for millions of timers.
  • Cancellation mechanism: lazy deletion with a cancelled flag or a separate set of cancelled timers.

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