Implement a Timer-like class that schedules a task to be executed at a specific time (focus on threads and concurrency)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-06-16
Problem Statement
Designing a Timer-like class that allows scheduling tasks to execute at a specific future time. Focus is on threading, concurrency, and accurate execution.
✅ Functional Requirements
1.Task Scheduling
•Schedule a task to run at a specific absolute time.
•Tasks can be one-time or recurring.
•Support scheduling multiple tasks simultaneously.
2.Task Execution
•Tasks execute as close as possible to their scheduled time.
•Support concurrent execution if multiple tasks are due at the same time.
3.Task Management
•Cancel a scheduled task.
•Reschedule a task to a different time.
•Query status of a task: scheduled, running, completed, canceled.
4.Concurrency & Thread Safety
•Allow multiple threads to schedule or cancel tasks concurrently.
•Ensure thread-safe access to internal task data structures.
5.Optional Features
•Support task priority for simultaneous executions.
•Persistent storage to recover scheduled tasks after a restart.
🔐 Constraints & Assumptions
•Focus on LLD, concurrency, and scheduling logic.
•Tasks are in-memory callbacks (Runnable or equivalent).
•Assume the system clock is reliable.
•Single-machine scheduler (not distributed).
🧱 Key Components & Classes
1.ScheduledTask Class
1
2class ScheduledTask {
3private:
4 string taskId;
5 DateTime executionTime;
6 function<void()> taskCallback;
7 string status; // SCHEDULED, RUNNING, COMPLETED, CANCELED
8
9public:
10 void execute();
11 void cancel();
12 void reschedule(DateTime newTime);
13};1.TimerScheduler Class
•Manages scheduling and execution of tasks.
1
2class TimerScheduler {
3private:
4 priority_queue<ScheduledTask*, vector<ScheduledTask*>, TaskComparator> taskQueue;
5 mutex schedulerMutex;
6 condition_variable cv;
7 bool running;
8
9public:
10 void schedule(ScheduledTask* task);
11 void cancelTask(string taskId);
12 void rescheduleTask(string taskId, DateTime newTime);
13 void start(); // Starts the scheduler thread
14 void stop(); // Stops the scheduler
15
16private:
17 void runScheduler(); // Internal thread that executes due tasks
18};1.TaskComparator Class
•Ensures tasks are ordered by executionTime and optionally priority.
1
2class TaskComparator {
3public:
4 bool operator()(ScheduledTask* t1, ScheduledTask* t2) {
5 return t1->executionTime > t2->executionTime; // min-heap
6 }
7};🔄 Example Workflow
1.User schedules a task → TimerScheduler.schedule(task) → added to priority queue.
2.Scheduler thread monitors the queue → sleeps until the next task’s execution time.
3.When time arrives, task is removed from queue and executed in a worker thread.
4.User can cancel or reschedule tasks → safely modify queue under lock.
🧪 Expected Deliverables
•Class diagram showing ScheduledTask, TimerScheduler, TaskComparator.
•Implementation of scheduling, execution, cancellation, and rescheduling.
•Test cases:
•Schedule tasks with different execution times
•Execute tasks at correct times
•Cancel and reschedule tasks
•Ensure thread-safety under concurrent operations
💡 Discussion Points
•Concurrency: mutex + condition variable for thread-safe queue access and efficient waiting.
•Timing accuracy: sleep until next task vs periodic wake-up.
•Extensibility: support recurring tasks, priority, and persistence.
•Performance: priority queue for O(log n) insertion/removal of tasks.