Nuro logoNuro
Coding·45 minFree preview

Periodic Job Scheduler (schedule / deschedule at N Hz)

Build a JobScheduler class with schedule(function, frequency_hz) and deschedule(...): the function must be invoked in the background at the given rate (e.g. 50 Hz → convert to a period) until it is descheduled. No template or function header is provided, you write your own tests, and the code must really run. Classic producer/consumer: one thread runs tasks while callers schedule and cancel from another. OS / concurrency questions follow.

SWE
concurrency
threading
scheduling
medium
Frequency
Medium
Last asked
2026-07-16
Stage
phone-screen · onsite-coding

Problem

Create a class that executes a user-provided function on a background schedule at a user-defined rate in hertz. The class exposes two methods:

  • schedule(function, frequency) — starts invoking function in the background at frequency Hz.
  • deschedule(...) — stops any further invocations of that function (also described as cancel).

you are asked to schedule a function with a certain frequency, and also be able to deschedule this function later, build a JobScheduler class.

Requirements:

  • Convert the frequency into a periodic interval (e.g. 50 Hz → one call every 20 ms) and keep invoking the function at that period.
  • Execution happens on a background thread; schedule / deschedule are called from another thread, so the shared schedule state must be thread-safe.
  • The code must actually run — a dry run is not accepted.

Pitfalls

  • There is no function header, template or test harness — you design the class API and write your own test cases from scratch.
  • Interviewers care more about a correct framework than every last detail; one candidate passed with code that never ran, but the structure (worker thread, locking, wake-up on change) was complete.
  • Don't busy-wait between invocations; sleep or wait on the condition variable until the next due time.
  • Decide and state what deschedule takes as its argument (the function itself or a handle returned by schedule) before coding — the prompt leaves it open.

Follow-ups

Operating-system and concurrency questions after the code is written (threads, locks, mutexes, memory management).

Preparation

  • Write a runnable JobScheduler in your interview language from a blank file in under 30 minutes, including a small test that schedules two functions at different rates and deschedules one.
  • Drill your language's primitives cold: thread creation, mutex, condition variable wait-with-timeout, and clean thread shutdown.
  • Practice explaining why a condition variable beats sleep polling for early wake-up on schedule changes.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...