Design Job Scheduler (Low-Level Design)
Run jobs when they are due, in a fixed order, with cancelling and (later) repeating jobs. Focus: Priority queues and a clock you control.
| Difficulty | MEDIUM |
|---|---|
| Design focus | Priority queues and a clock you control |
| Automated grading | 3 public + 8 hidden tests (11 total) |
| Phase 2 follow-up | Repeating jobs |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Jobs have an id, a time to run (runAt, in ms) and a priority (higher runs first, default 0).
tick(now) runs every job whose runAt is at or before now and returns their ids in run order: earlier runAt first, then higher priority, then the order they were scheduled.
Lots of jobs may be waiting, so finding the next due job should not mean sorting everything on every tick.
Functional requirements
- schedule(id, runAt, priority?) adds a job; an id that is already waiting throws.
- cancel(id) removes a waiting job and returns true, or false if there is none.
- tick(now) returns the ids it ran; pending() counts waiting jobs; nextRunAt() is the earliest waiting runAt, or null.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
class JobScheduler {
schedule(id: string, runAt: number, priority?: number): void {
throw new Error("Not implemented");
}
cancel(id: string): boolean {
throw new Error("Not implemented");
}
/** Runs every due job; returns their ids in run order */
tick(now: number): string[] {
throw new Error("Not implemented");
}
pending(): number {
throw new Error("Not implemented");
}
nextRunAt(): number | null {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 8 hidden edge-case tests in a sandbox:
Runs jobs that are due
var s = new JobScheduler();
s.schedule("a", 100); s.schedule("b", 50); s.schedule("c", 200);
assertEqual(s.tick(150), ["b", "a"]);
assertEqual(s.pending(), 1);
Priority breaks ties
var s = new JobScheduler();
s.schedule("low", 10, 1); s.schedule("high", 10, 5);
assertEqual(s.tick(10), ["high", "low"]);
Cancel
var s = new JobScheduler();
s.schedule("a", 10);
assertEqual(s.cancel("a"), true);
assertEqual(s.cancel("a"), false);
assertEqual(s.tick(100), []);
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- Waiting jobs are kept in a priority queue (heap) or another ordered structure: A heap gives the next job in O(log n). Sorting all jobs on every tick gets slow as the queue grows.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 11 tests, you unlock the change request: Repeating jobs.
Pair with High-Level System Design
Ready to scale Job Scheduler across distributed servers? Try the matching HLD simulator problem: Design Job Scheduler.
Relevant low-level design lessons
Choose the structure for the operation
Pick a data structure from the operations a class must make fast.
Map plus ordered list: the LRU trick
Build constant-time get, put and evict-oldest.
Ordering with ties
Write one comparison rule that every part of the code agrees on.
More low-level design problems
Design Calendar Scheduling
Book meetings without double-booking anyone, and find free time for a group.
Design Elevator System
Several elevators answering hall calls, moving one floor per step and serving stops in their direction of travel.
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Rate Limiter
Two rate-limiting algorithms behind one interface, so callers can swap them.