Design a Delayed Task Scheduler
⏰ What is a delayed task scheduler? It is the part of a program that says "run this in 5 seconds" or "run this every minute". Callers hand it a task and a delay, and a background thread runs each task when its time comes. Java's
ScheduledExecutorService, Python'sschedmodule and the timers inside every web server are all versions of it.
Interviewers like this problem because it is small but full of real concurrency. There is a shared data structure (the queue of waiting tasks), a background thread that must sleep exactly until the right moment, callers on other threads that can add an earlier task or cancel one while that thread sleeps, and a repeating task whose next run time is easy to get subtly wrong.
Requirements
"Design a scheduler that runs tasks after a delay, like Java's ScheduledExecutorService. Tasks can also repeat at a fixed rate, and callers can cancel them."
Most of the design hides in what "after a delay" and "repeat" mean exactly. The questions below settle it.
Clarifying Questions
You: Which thread runs the tasks? Should every task get its own thread?
Interviewer: No. One background thread, the timer thread, runs the tasks in the order they become due. We can talk about a pool of workers later.
You: If two tasks are due at exactly the same time, which runs first?
Interviewer: The one that was scheduled first.
You: For repeating tasks, is the period measured from when the last run was due, or from when it finished?
Interviewer: From when it was due. That is fixed rate: a task every 30 seconds runs at 30, 60, 90, even if one run was late. If the scheduler falls behind, it runs the missed runs back to back.
You: What does cancel do to a task that already ran, or one that is running right now?
Interviewer: Cancel stops all future runs and tells the caller whether there was anything left to stop. It never interrupts a run in progress.
You: How do we know the time? Should the scheduler read the system clock?
Interviewer: In production, yes. For the exercise, give the scheduler a clock that only moves when we call advanceTo(time). Then the tests are exact instead of timing-dependent.
You: Do many threads call schedule and cancel at the same time?
Interviewer: Yes. Any thread may schedule or cancel at any moment, including while the timer thread is running a task.
You: What happens on shutdown?
Interviewer: Stop the timer thread, drop everything still waiting, and report how many tasks were dropped. Reject new tasks after that.
The clock answer shapes the whole design. A scheduler that reads time.monotonic() directly can only be tested with sleep, which is slow and flaky. If time comes from one place, the production version and the test version differ only in how the timer thread learns that time has moved.
💡 Interview tip: Ask "fixed rate or fixed delay?" out loud. They are different features with different bugs, and most candidates do not know the difference until the interviewer asks. Naming it early shows you have used real schedulers.
Final Requirements
schedule(name, delay)runs a task once atnow + delayand returns a task id.scheduleAtFixedRate(name, initialDelay, period)runs a task atnow + initialDelay, then everyperiodafter the time it was due. A run that falls behind is caught up, never skipped, and a task never overlaps itself.- Tasks run on one timer thread, in order of due time; equal times run in the order the tasks were scheduled.
cancel(id)stops all future runs and returns whether the task still had a run left.pending()returns how many tasks will still run at least once.scheduleandcancelare safe to call from any thread at any time.shutdown()stops the timer thread, drops waiting tasks and returns how many it dropped. Later calls toschedulefail with-1.- Time comes from one clock. In the testable version,
advanceTo(time)moves it and returns the names of the tasks that ran.
Out of scope
- Persisting tasks across restarts, or running them on several machines.
- Cron expressions ("every Monday at 9").
- Task results and futures (we come back to them in Extensibility).
- Priorities other than due time.