Skip to content
Preptima
hardCodingDesignMidSenior

Design an in-memory job scheduler that runs one-off, delayed and recurring jobs. Write the core classes.

An in-memory job scheduler keeps one queue ordered by next run time, such as a DelayQueue, and one dispatcher thread that hands due entries to a worker pool. Recurring jobs are re-queued after each run, cancellation is a generation check on dequeue, and missed runs need a stated skip or catch-up policy.

6 min readUpdated 2026-10-10

Answer

Short answer

Order the work by time. An in-memory job scheduler keeps every pending run in one queue sorted by its next run time, typically a DelayQueue, with a single dispatcher thread that blocks until the head is due and hands it to a worker pool. A one-off job is a run with no successor, a delayed job is a one-off run with a later start, and a recurring job computes its next run time when the current one finishes. Cancellation marks the job dead so a stale entry is dropped on dequeue, and missed runs get a policy you state out loud.

Was this answer helpful?

Likely follow-ups

  • A recurring job takes 90 seconds and its period is 60. Walk me through the next three run times under your policy.
  • How would you unit-test a job that runs every night at 02:00 without waiting for 02:00?
  • Add cron expressions. Which of your classes change, and which stay untouched?
  • The process restarts. Which jobs are lost, and what is the smallest change that brings them back?

Related questions

Further reading