Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement a thread-safe periodic job scheduler
Problem: Periodic Job Scheduler
Implement a small, in-process job scheduler capable of executing periodic tasks (functions or callbacks) at a specified frequency.
Requirements
- Task Definition: The scheduler must support scheduling tasks defined as functions or callbacks.
- Periodicity: Each task is periodic, specified by a frequency (e.g., 50 Hz, meaning 50 times per second). The implementation must convert this frequency into a time interval (period).
- Thread-Safe API: Expose the following thread-safe public APIs:
schedule(taskFn, frequencyHz) -> jobId: Schedules a new periodic task and returns a unique identifier for it.cancel(jobId) -> void: Cancels a previously scheduled task using its identifier.
- Concurrency Model: The implementation must be fully runnable and handle concurrency:
- At least one dedicated worker thread must be responsible for executing scheduled tasks.
schedule()andcancel()methods must be callable concurrently from other threads while the worker thread(s) are actively running tasks.
Behavioral Expectations and Assumptions
- Timing Source: A monotonic clock must be used for all timing operations.
- Multiple Jobs: The scheduler must support multiple jobs running concurrently.
- Cancellation Semantics: Once a job is cancelled, it must not execute again.
- Task Overrun Policy: The implementation must explicitly define and handle the behavior when a task's execution time exceeds its scheduled period (e.g., whether to skip subsequent ticks to maintain frequency or to execute immediately to "catch up").
Deliverables
Candidates are expected to provide:
- The core data structures used by the scheduler and the chosen synchronization strategy.
- The logic for the worker thread's main loop, including its sleep/wake mechanism.
- A detailed explanation of how task cancellation is handled safely under concurrent access.
AI interview prep
Core dilemma
Balancing precise, timely execution of periodic tasks with robust thread safety, efficient resource utilization, and predictable behavior under varying task loads and durations.
Mid-level signals3
Senior signals4
Red flags4
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a thread-safe periodic job scheduler
Problem: Periodic Job Scheduler
Implement a small, in-process job scheduler capable of executing periodic tasks (functions or callbacks) at a specified frequency.
Requirements
- Task Definition: The scheduler must support scheduling tasks defined as functions or callbacks.
- Periodicity: Each task is periodic, specified by a frequency (e.g., 50 Hz, meaning 50 times per second). The implementation must convert this frequency into a time interval (period).
- Thread-Safe API: Expose the following thread-safe public APIs:
schedule(taskFn, frequencyHz) -> jobId: Schedules a new periodic task and returns a unique identifier for it.cancel(jobId) -> void: Cancels a previously scheduled task using its identifier.
- Concurrency Model: The implementation must be fully runnable and handle concurrency:
- At least one dedicated worker thread must be responsible for executing scheduled tasks.
schedule()andcancel()methods must be callable concurrently from other threads while the worker thread(s) are actively running tasks.
Behavioral Expectations and Assumptions
- Timing Source: A monotonic clock must be used for all timing operations.
- Multiple Jobs: The scheduler must support multiple jobs running concurrently.
- Cancellation Semantics: Once a job is cancelled, it must not execute again.
- Task Overrun Policy: The implementation must explicitly define and handle the behavior when a task's execution time exceeds its scheduled period (e.g., whether to skip subsequent ticks to maintain frequency or to execute immediately to "catch up").
Deliverables
Candidates are expected to provide:
- The core data structures used by the scheduler and the chosen synchronization strategy.
- The logic for the worker thread's main loop, including its sleep/wake mechanism.
- A detailed explanation of how task cancellation is handled safely under concurrent access.