Design a Delayed Scheduler

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-10-14

Problem Statement

Designing a delayed task scheduler that executes tasks after a specified delay or at a specific future time. Focus is on object-oriented design, concurrency, task management, and timely execution.


✅ Functional Requirements


1.Task Scheduling
•Schedule a task to run after a delay or at a specific timestamp.
•Tasks can be one-time or recurring.
•Each task has metadata: taskId, executionTime, priority, and callback.
2.Task Execution
•Execute tasks as close as possible to their scheduled time.
•Support concurrent execution of multiple tasks scheduled at the same time.
3.Task Management
•Cancel a scheduled task.
•Reschedule a task with a new execution time.
•Query task status: scheduled, running, completed, canceled.
4.Concurrency & Thread Safety
•Handle multiple threads scheduling and executing tasks concurrently.
•Ensure thread-safe operations while adding, removing, or executing tasks.
5.Optional Features
•Task persistence for system restarts.
•Priority-based execution if multiple tasks have the same execution time.


🔐 Constraints & Assumptions


•Focus on LLD, class design, and thread-safe scheduling, not distributed deployment.
•Tasks are small, in-memory callbacks or functions.
•Assume the system clock is reliable.


🧱 Key Components & Classes


1.Task Class
1 2class Task { 3private: 4 string taskId; 5 DateTime executionTime; 6 int priority; 7 function<void()> callback; 8 string status; // SCHEDULED, RUNNING, COMPLETED, CANCELED 9public: 10 void execute(); 11 void cancel(); 12 void reschedule(DateTime newTime); 13};
1.TaskScheduler Class
1 2class TaskScheduler { 3private: 4 priority_queue<Task*, vector<Task*>, TaskComparator> taskQueue; // min-heap by executionTime 5 mutex schedulerMutex; 6 condition_variable cv; 7 bool running; 8public: 9 void scheduleTask(Task* task); 10 void cancelTask(string taskId); 11 void rescheduleTask(string taskId, DateTime newTime); 12 void start(); // starts scheduler thread 13 void stop(); // stops scheduler thread 14private: 15 void executeTasks(); // internal thread function 16};
1.TaskComparator Class
•Orders tasks by executionTime and priority.
1 2class TaskComparator { 3public: 4 bool operator()(Task* t1, Task* t2) { 5 if (t1->executi= t2->executionTime) 6 return t1->priority < t2->priority; 7 return t1->executionTime > t2->executionTime; 8 } 9};


🔄 Example Workflow


1.User schedules a task → TaskScheduler.scheduleTask(task) adds it to the priority queue.
2.Scheduler thread waits for the earliest task → wakes up when execution time is reached.
3.Task executes → status updated to RUNNING and then COMPLETED.
4.User cancels or reschedules a task → modifies the queue safely.


🧪 Expected Deliverables


•Class diagram showing Task, TaskScheduler, TaskComparator.
•Implementation of task scheduling, execution, cancellation, and rescheduling.
•Test cases:
•Schedule multiple tasks with different delays
•Execute tasks at correct times
•Cancel or reschedule tasks
•Ensure thread-safety under concurrent scheduling


💡 Discussion Points


•Concurrency: thread-safe access to the task queue using mutex and condition variables.
•Accuracy: tasks execute close to scheduled time, considering system clock and thread wake-up latency.
•Extensibility: support recurring tasks, persistent storage, and distributed scheduling.
•Performance: priority queue / min-heap for efficient retrieval of the next task.


Solution

Comments