← Nuro Interview InsightsSoftware Engineer

Nuro·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026

Summary

Nuro asked me to build a working periodic job scheduler from scratch, not just sketch it on a whiteboard. Real code, real threads, real timing. It was more involved than I expected for a single interview question.

Questions Asked (1)

Q1

Implement a periodic job scheduler with schedule(task, hz) and cancel(handle) APIs. Convert a frequency in Hz to a time interval and run tasks at that cadence. Use one thread for execution and allow scheduling and cancellation from a separate thread concurrently. The code must actually run and handle thread-safety, clean shutdown, and accurate timing.

System DesignTechnical Trade-offsAlgorithms & Data Structures
Author's notes

The frequency-to-interval conversion is trivial but I almost got sloppy there and moved on too fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and constraints, then outline a design using a priority queue (min-heap) of scheduled tasks ordered by next execution time, with a dedicated worker thread that sleeps until the next task's deadline. Discuss thread-safety using a mutex and condition variable for scheduling/cancellation, and handle clean shutdown with a stop flag and thread join. Finally, address timing accuracy and potential drift, and mention trade-offs like using a timer wheel for high-frequency tasks.

Pro tip: Emphasize that you would use a monotonic clock (e.g., std::chrono::steady_clock) to avoid issues with system time changes, and that you would compute the next execution time based on the scheduled time rather than the current time to prevent drift accumulation.

1. Clarify Requirements and Constraints

Ask about expected number of tasks, frequency range, precision requirements, and whether tasks can be scheduled/cancelled from multiple threads. Confirm that the scheduler should run tasks at a fixed rate (period) rather than fixed delay.

2. Design Data Structures and Threading Model

Propose a min-heap (priority queue) keyed by next execution time, protected by a mutex. Use a single worker thread that waits on a condition variable until the next task is due or a new task is scheduled/cancelled.

3. Implement Schedule and Cancel APIs

schedule(task, hz) computes interval = 1/hz seconds, inserts a task with next_run = now + interval, and notifies the worker. cancel(handle) marks the task as cancelled and removes it from the heap (or lazily skips it).

4. Handle Thread-Safety and Clean Shutdown

Use a mutex to protect the heap and a condition variable for signaling. For shutdown, set a stop flag, notify the worker, and join the thread. Ensure no tasks are executed after shutdown.

5. Address Timing Accuracy and Trade-offs

Discuss using steady_clock, computing next_run based on previous scheduled time to avoid drift, and handling long-running tasks (e.g., if a task takes longer than its period). Mention alternative designs like timer wheels for scalability.

Key Points to Mention

  • Use of a priority queue (min-heap) to efficiently find the next task to execute.
  • Thread-safety mechanisms: mutex and condition variable for synchronization between scheduler and worker threads.
  • Clean shutdown: stop flag, condition variable notification, and joining the worker thread.
  • Accurate timing: use steady_clock, compute next execution time from scheduled time to prevent drift.
  • Handling cancellation: either remove from heap or mark as cancelled and skip during execution.
  • Trade-offs: single-threaded execution vs. thread pool, timer wheel vs. heap for high-frequency tasks.

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