KatInterview

Build a Display Order for Tasks with Dedup, Filtering and Subtasks

Trees & GraphsModel answer
MediumModel answerTrees & Graphssortinghashingtree-traversal

Last updated

Turn a flat batch of scheduler tasks into a display list: merge duplicates, hide finished work, order by priority, and keep each subtask right under its parent.

You are building the "what should I look at next" view of a task scheduler. You receive a flat batch of tasks and must return them in display order.

Each task has a unique id, a description, a due_date, a numeric priority, a completed flag, and an optional parent_id. When parent_id is set, the task is a subtask of the task with that id.

from dataclasses import dataclass
from datetime import date
from typing import Optional

@dataclass
class Task:
    id: int
    description: str
    due_date: date
    priority: int
    completed: bool
    parent_id: Optional[int]   # None for a top-level task

def build_schedule(tasks: list[Task]) -> list[Task]:
    ...

The display list is produced by these steps, in this order:

  1. Merge duplicates. Two tasks are duplicates if they share the same (description, due_date).
  2. Remove tasks that are already completed.
  3. Sort what remains by a priority rule (your interviewer or product owner defines it, so ask).
  4. Put every subtask directly after its parent.

Part 1: Implement build_schedule. Before coding, list the questions you would ask and state the answer you will assume for each. At minimum, think about:

  • Which direction of priority is more urgent, and how are ties broken?
  • Which copy survives when duplicates differ (one completed, different priorities, different parents)? What happens to subtasks that pointed at a removed copy?
  • What happens to a subtask whose parent was removed or never existed?
  • Can subtasks be nested more than one level deep? Are siblings sorted by the same rule?
  • Can parent_id links form a loop, and is that an error?
  • Does the order of the steps ever change the result?

State the time complexity of your solution.

Part 2: Working with an AI coding assistant. Suppose the environment provides an AI assistant and you are expected to use it as you would at work. Describe what you would delegate to it, what you would decide yourself, and how you would verify what it produces.

Hints

Hint 1

Write down the policy decisions first (priority direction, tie-break, which duplicate survives, orphan and cycle behavior). Once they are fixed, the code is mostly mechanical.

Hint 2

Order matters: if a completed copy and a pending copy are duplicates, picking an arbitrary survivor before dropping completed tasks can make a pending task disappear. Decide the survivor rule deliberately, and build a map from every removed id to the surviving id so parent links can be redirected.

Hint 3

Sort once globally, then bucket tasks into `children[parent_id]` (or `roots`) in that sorted order so every sibling list is already ordered. Emit with an iterative depth-first walk from the roots, and if some kept task was never emitted, the input has a cycle.