KatInterview

Schedule Dependent Tasks with Priority and Tag Exclusion

Trees & GraphsAuto-graded · 19 test cases
MediumAuto-gradedTrees & Graphstopological-sortheapgraphs

Last updated

Tests Kahn's algorithm with deterministic tie-breaking, urgency-based heap ordering, cycle detection with path reporting, and transparent skipping of tag-excluded tasks.

You are building a task runner that dispatches work items in a safe order. Each task may depend on other tasks that must finish first, carry an urgency score, and belong to zero or more named groups.

A task is described by a dictionary with these fields:

{"id": "test", "needs": ["compile"], "urgency": 4, "tags": ["slow"]}
  • needs — list of task ids that must appear before this task in the output.
  • urgency — integer priority; higher means more important.
  • tags — optional list of string labels.

Implement:

def schedule_tasks(tasks, by_urgency=False, excluded_tags=[]) -> list[str]

Ordering rules

Default mode (by_urgency=False): Among all tasks whose dependencies have already been scheduled, always pick the one that appears earliest in the input list. Use a min-heap keyed on input position to ensure this is deterministic.

Urgency mode (by_urgency=True): Among ready tasks, pick the one with the highest urgency. Break ties by input position (earlier wins). Urgency never lets a task leapfrog unmet dependencies — a task only becomes a candidate once every task it needs has been scheduled.


Tag exclusion

When excluded_tags is a non-empty list, any task whose tags overlap with that list is omitted from the output. Excluded tasks are still used internally to resolve ordering: if task D needs excluded task B, and B needs task A, then D must still appear after A in the output (the excluded task acts as a transparent bridge).

The full dependency graph — including excluded tasks — must be validated and scheduled internally. Only the final output list is filtered.


Error handling

Raise a ValueError for any of these conditions:

  • Duplicate ids: two or more tasks share the same id.

  • Unknown prerequisite: a task lists a needs entry that does not match any task id.

  • Cycle: dependencies form a loop. The error message must include one concrete cycle path, for example:

    cycle: x -> z -> y -> x
    

Constraints

  • 1 ≤ number of tasks ≤ 10⁴
  • 0 ≤ number of dependency edges ≤ 10⁵
  • urgency is a non-negative integer; defaults to 0 if omitted.
  • tags defaults to [] if omitted.
  • needs defaults to [] if omitted.
  • Task ids are non-empty strings.
  • Output must be fully deterministic for a given input.
  • Aim for O((V + E) log V) time.

Examples

Example 1

Input: schedule_tasks([{'id': 'fetch', 'needs': [], 'urgency': 1}, {'id': 'compile', 'needs': ['fetch'], 'urgency': 3}, {'id': 'lint', 'needs': ['fetch'], 'urgency': 5, 'tags': ['style']}, {'id': 'test', 'needs': ['compile'], 'urgency': 4}, {'id': 'docs', 'needs': [], 'urgency': 2, 'tags': ['docs']}, {'id': 'package', 'needs': ['test', 'lint'], 'urgency': 2}, {'id': 'publish', 'needs': ['package', 'docs'], 'urgency': 9}], False, [])

Output: ['fetch', 'compile', 'lint', 'test', 'docs', 'package', 'publish']

Example 2

Input: schedule_tasks([{'id': 'fetch', 'needs': [], 'urgency': 1}, {'id': 'compile', 'needs': ['fetch'], 'urgency': 3}, {'id': 'lint', 'needs': ['fetch'], 'urgency': 5, 'tags': ['style']}, {'id': 'test', 'needs': ['compile'], 'urgency': 4}, {'id': 'docs', 'needs': [], 'urgency': 2, 'tags': ['docs']}, {'id': 'package', 'needs': ['test', 'lint'], 'urgency': 2}, {'id': 'publish', 'needs': ['package', 'docs'], 'urgency': 9}], True, [])

Output: ['docs', 'fetch', 'lint', 'compile', 'test', 'package', 'publish']

Example 3

Input: schedule_tasks([{'id': 'fetch', 'needs': [], 'urgency': 1}, {'id': 'compile', 'needs': ['fetch'], 'urgency': 3}, {'id': 'lint', 'needs': ['fetch'], 'urgency': 5, 'tags': ['style']}, {'id': 'test', 'needs': ['compile'], 'urgency': 4}, {'id': 'docs', 'needs': [], 'urgency': 2, 'tags': ['docs']}, {'id': 'package', 'needs': ['test', 'lint'], 'urgency': 2}, {'id': 'publish', 'needs': ['package', 'docs'], 'urgency': 9}], False, ['docs', 'style'])

Output: ['fetch', 'compile', 'test', 'package', 'publish']

Hints

Hint 1

Build an indegree count and a children map from the dependency edges, then use a heap as your ready queue — the heap key switches between input position and (-urgency, input_position) depending on the mode.

Hint 2

For cycle detection, run Kahn's algorithm to completion first: any task not in the output is part of or blocked by a cycle. Then do a DFS restricted to those leftover tasks to extract one concrete path.

Hint 3

Tag exclusion does not require a separate graph pass — schedule everything internally, then filter the output list. The internal run already respects transitive ordering through excluded tasks, so skipping them in the output is safe.