InterviewDB Question · Los Angeles

Task List: Design a Thread-Safe Task Queue with Priority Scheduling

Question Details

Problem

Design a task scheduling system that supports: adding tasks with a priority (higher number = higher priority), popping the highest-priority task, and querying the count of pending tasks. The system must be safe for concurrent access from multiple threads.

python
import threading

class TaskList:
    def __init__(self): ...
    def add_task(self, task_id: str, priority: int) -> None: ...
    def pop_task(self) -> str | None: ...

**Returns** task_id or None if empty
    def pending_count(self) -> int: ...

Example:

tl = TaskList()
tl.add_task("A", priority=5)
tl.add_task("B", priority=10)
tl.add_task("C", priority=1)
tl.pop_task()  -> "B"
tl.pop_task()  -> "A"
tl.pending_count()  -> 1

Follow-ups

  1. What synchronization primitive do you use to protect the heap, and why is a lock sufficient here vs. a condition variable?
  2. How would you implement pop_task as a blocking call that waits until a task is available?
  3. What happens if two tasks have the same priority? How do you ensure FIFO ordering among them?
  4. How would you add a cancel_task(task_id) operation efficiently without rebuilding the heap?

Full Details

Problem

Design a task scheduling system that supports: adding tasks with a priority (higher number = higher priority), popping the highest-priority task, and querying the count of pending tasks. The system must be safe for concurrent access from multiple threads.

python
import threading

class TaskList:
    def __init__(self): ...
    def add_task(self, task_id: str, priority: int) -> None: ...
    def pop_task(self) -> str | None: ...

**Returns** task_id or None if empty
    def pending_count(self) -> int: ...

Example:

tl = TaskList()
tl.add_task("A", priority=5)
tl.add_task("B", priority=10)
tl.add_task("C", priority=1)
tl.pop_task()  -> "B"
tl.pop_task()  -> "A"
tl.pending_count()  -> 1

Follow-ups

  1. What synchronization primitive do you use to protect the heap, and why is a lock sufficient here vs. a condition variable?
  2. How would you implement pop_task as a blocking call that waits until a task is available?
  3. What happens if two tasks have the same priority? How do you ensure FIFO ordering among them?
  4. How would you add a cancel_task(task_id) operation efficiently without rebuilding the heap?

About This Question

This is a reported interview question from a vanta interview during the phone round.

It covers the following topics: Heap, Phone, Coding, Queue, Onsite .