Problem statement
Design a delay queue: a thread-safe structure where each element becomes available only after its delay elapses, and consumers block until the earliest-due item is ready. It backs an in-process scheduler that hands due tasks to worker threads.
Operating context. One process. Producers call offer(task, delay) to schedule a task with a fire time; consumer threads call take() and receive the earliest-due task, blocking until it is due even when the queue is non-empty. Items are ordered by due time (a min-heap). To avoid a herd of consumers all timing on the head, only one consumer waits on the head's timer while the rest wait unbounded. Tasks can be cancelled or rescheduled before they fire.
Out of scope. Actually executing the tasks (an executor's job), distributed or cron scheduling across machines, durable timers that survive a restart, cron-expression parsing, and missed-fire catch-up policy.
What to produce. The class model (the delay queue, the Delayed item, the priority-ordered store, and the timing/wait coordination), the public API, and the state transitions of a task (scheduled, due, taken, cancelled). Be explicit about how a consumer waits precisely until the head becomes due without busy-polling, how an earlier-due insert wakes a consumer already waiting on a later head, and how the leader-follower waiting avoids a thundering herd.
Requirements
This assessment is a Premium feature.
The statement above is free to read. The functional and non-functional requirements, and the graded canvas that scores your design against them, come with Premium.
Topics
- System Design LLD
- Concurrency Scheduling
- Concurrency Condition-Variables
- Patterns Priority-Queue
- Oop Interface-Design