KatInterview

Build a Min-Heap and Schedule Tasks by Priority

Heaps, Sorting & SearchAuto-graded · 21 test cases
HardAuto-gradedHeaps, Sorting & Searchheaptopological-sortgraph

Last updated

Construct a working min-heap from scratch, then apply it to schedule dependent tasks in lexicographic order while detecting cycles.

You will solve two connected problems in a single session.

Part 1: Custom Min-Heap Implementation

Build a MinHeap class that maintains elements in min-heap order. Your implementation must support:

  • push(x): Add element x to the heap
  • pop(): Remove and return the minimum element
  • peek(): Return the minimum element without removing it
  • __len__(): Return the count of elements currently in the heap
  • A constructor accepting an optional iterable of initial values, building the heap in O(n) time

Your heap should handle comparable values like integers or tuples. When the heap is empty, pop() and peek() should raise IndexError.

Requirements:

  • Implement the heap using a list-based binary tree representation
  • Do not use heapq, queue.PriorityQueue, or any other built-in priority queue
  • Do not sort the entire collection on every operation
  • The constructor must run in linear time when given initial values

Part 2: Task Scheduling with Dependencies

You have n tasks numbered from 0 to n - 1. Some tasks depend on others: you are given a list of dependency pairs [task, required], where required must be finished before task can start.

Complete all tasks one at a time. When multiple tasks are ready (all their dependencies are satisfied), always choose the one with the smallest number. Return the completion order as a list of task numbers.

If the dependencies form a cycle that prevents completing all tasks, return an empty list.

You must use the MinHeap class from Part 1 to determine which task to complete next.

Example:

  • With n = 3 and dependencies [[0, 1]], task 1 must finish before task 0
  • Tasks 1 and 2 start ready (no dependencies)
  • Choose task 1 (smaller number), which unlocks task 0
  • Task 0 is now the smallest ready task
  • Finally complete task 2
  • Result: [1, 0, 2]

Constraints

  • 1 ≤ n ≤ 2000
  • 0 ≤ dependencies.length ≤ 5000
  • Each dependency pair [a, b] satisfies 0 ≤ a, b < n
  • The input may contain duplicate dependency pairs
  • A task will not list itself as its own dependency in valid test cases

Examples

Example 1

Input: schedule_tasks(3, [[0, 1]])

Output: [1, 0, 2]

Example 2

Input: schedule_tasks(4, [[1, 0], [2, 0], [3, 1], [3, 2]])

Output: [0, 1, 2, 3]

Example 3

Input: schedule_tasks(2, [[0, 1], [1, 0]])

Output: []

Hints

Hint 1

For the heap: remember that in array index i, the children are at 2i+1 and 2i+2, and the parent is at (i-1)//2

Hint 2

Linear-time heap construction works by starting from the last non-leaf node and sifting down, rather than inserting elements one by one

Hint 3

For task scheduling: track how many dependencies each task has remaining, and use the heap to always pick the smallest task number among those with zero remaining dependencies

Hint 4

If you finish fewer than n tasks, some tasks were blocked by circular dependencies