DesignTheSystem/dev
MEDIUM Low-Level Design (LLD) 11 automated tests

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.

DifficultyMEDIUM
Design focusPriority queues and a clock you control
Automated grading3 public + 8 hidden tests (11 total)
Phase 2 follow-upRepeating jobs
LanguageTypeScript (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 (core)

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 (core)

var s = new JobScheduler();
s.schedule("low", 10, 1); s.schedule("high", 10, 5);
assertEqual(s.tick(10), ["high", "low"]);

Cancel (core)

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

More low-level design problems