Skip to the guide

Design a task scheduler

AdvancedCase study11 pages

The interviewer asks you to design the classes of a task scheduler: run this task in 30 seconds, run that one in an hour, and retry what fails. Picture a bakery's order rail, with each order on a card in time order. The head baker reads the top card, sets one kitchen timer and sits down. When it rings, she hands the card to a free cook. When someone adds an order due sooner, they tap her on the shoulder, and she reads the rail again.

It is also asked as "design a delayed job queue" or "design a delayed job scheduler". The closest other case study is the pub/sub broker, which also shares a pool of workers behind one lock. Interviewers like it because it comes down to one scene: a sooner task arriving while a thread sleeps. This page is the class-level part. Scheduling work for later owns jobs in a database, and Distributed job scheduler owns many machines.

Below is a full 45-minute round on the LLD framework. You are the candidate. At each Guess before you read on, stop and answer first. The full runnable project, with its tests, is a download in Java, JavaScript and Python.

Run the full project

This design as a project you can run, with its tests and a demo. You only need Java 21, Node 24 or Python 3.13. The README says how to run it.

Step 1 · Clarify the requirements and cut the scope

Minutes 0–5 · How this step works

Interviewer: Design a task scheduler.

You: What is a task, and when does it run?

Interviewer: Code with no arguments, after a delay of at most a year.

You: What if a task throws?

Interviewer: The caller chooses: no retry, or a set number of attempts with a fixed wait.

You: Can a caller cancel a task?

Interviewer: Yes, if it isn't running.

You: How big is it?

Interviewer: Up to 100,000 waiting. At peak about 50 fall due a second, and a task takes about 100 ms.

You: Then I'll build schedule, cancel and stateOf, one thread that waits for the next job, and a pool of workers. A full queue refuses a job. Repeating tasks, priorities, disk and many machines are out. OK?

Interviewer: Good.