Wake a Timed Job Scheduler When Its Earliest Task Changes

Quick Overview

Implement a timed scheduler that sleeps until its earliest deadline and wakes when an earlier task is inserted. Explain heap synchronization, condition waits, due-task dispatch, worker handoff, and clock semantics without busy polling.

Wake a Timed Job Scheduler When Its Earliest Task Changes

Company: Xiaohongshu

Role: Backend Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Implement the scheduling logic for timed jobs using a priority queue. When a new job becomes the earliest queued job, wake the scheduler so it can reset its wait. Otherwise, let the current timer wake it. On waking, dispatch all due jobs to workers and wait for the next deadline. ### Constraints & Assumptions - New tasks can arrive while the scheduler is waiting. - The queue is ordered by scheduled execution time. - Clock semantics, equal-deadline order, worker capacity, and persistence requirements must be clarified. - This is a concurrent scheduling exercise, not a supplied deterministic batch function. ### Clarifying Questions to Ask - Are deadlines absolute wall-clock times or relative delays? - Can jobs be canceled or rescheduled, or only inserted? - What happens if the worker queue cannot accept a due job immediately? ```hint Checking the next deadline and entering the wait must coordinate with insertion; otherwise an earlier task can arrive between those operations and its wake-up can be lost. ``` ### What a Strong Answer Covers Use a min-heap and a synchronized condition or equivalent timer protocol. Explain the insertion wake rule, atomic wait setup, rechecking after every wake, dispatch of all due tasks, and execution outside the scheduling lock. ### Follow-up Questions - Why must the wait be inside a loop even without a new task? - How would deleting the current earliest task affect the timer? - How would a wall-clock adjustment affect absolute deadlines?

Overview: Implement a timed scheduler that sleeps until its earliest deadline and wakes when an earlier task is inserted. Explain heap synchronization, condition waits, due-task dispatch, worker handoff, and clock semantics without busy polling.

Xiaohongshu logo
Xiaohongshu
Oct 31, 2026
mediumBackend EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

Implement the scheduling logic for timed jobs using a priority queue. When a new job becomes the earliest queued job, wake the scheduler so it can reset its wait. Otherwise, let the current timer wake it. On waking, dispatch all due jobs to workers and wait for the next deadline.

Constraints & Assumptions

  • New tasks can arrive while the scheduler is waiting.
  • The queue is ordered by scheduled execution time.
  • Clock semantics, equal-deadline order, worker capacity, and persistence requirements must be clarified.
  • This is a concurrent scheduling exercise, not a supplied deterministic batch function.

Clarifying Questions to Ask Guidance

  • Are deadlines absolute wall-clock times or relative delays?
  • Can jobs be canceled or rescheduled, or only inserted?
  • What happens if the worker queue cannot accept a due job immediately?

What a Strong Answer Covers Guidance

Use a min-heap and a synchronized condition or equivalent timer protocol. Explain the insertion wake rule, atomic wait setup, rechecking after every wake, dispatch of all due tasks, and execution outside the scheduling lock.

Follow-up Questions Guidance

  • Why must the wait be inside a loop even without a new task?
  • How would deleting the current earliest task affect the timer?
  • How would a wall-clock adjustment affect absolute deadlines?