KatInterview

Hierarchical Task Tracker: OO Domain Model & Service Layer

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented designtree traversalAPI design

Last updated

Design and implement an object-oriented model for a nested task manager with status propagation, recursive deletion, and attribute-based filtering.

Hierarchical Task Tracker

Build the domain model and service layer for a nested task management tool — think of it like the checklist feature in a collaborative doc editor, where tasks can contain sub-tasks at any depth.

What you need to model

Task attributes

Each task has:

  • a required title and an optional description
  • a priority: LOW, MEDIUM, or HIGH (default MEDIUM)
  • an optional due_date
  • zero or more string tags
  • a status: PENDING, IN_PROGRESS, or COMPLETED (starts as PENDING)
  • an optional reference to a parent task (making it a subtask)

Tasks can be nested arbitrarily deep. A task with no parent is a root task.

Status propagation rules

  1. Marking a task COMPLETED must also mark every descendant COMPLETED.
  2. Marking a task PENDING or IN_PROGRESS must move every ancestor that is currently COMPLETED to IN_PROGRESS.

These two rules must never trigger each other in a loop.

Deletion

Deleting a task removes it and every descendant from the system. Any external ID referring to a deleted task should produce a clear error, not a silent no-op or a stale object.

Querying

Provide a way to search across all tasks with any combination of: status, a single tag, priority. Results should be sortable by due_date (tasks without a date sort last) or by priority (HIGH first). An unrecognised sort key should raise an error.

Interface to implement

from enum import Enum
from typing import List, Optional, Set
from datetime import datetime

class Priority(Enum):
    LOW = 1
    MEDIUM = 2
    HIGH = 3

class Status(Enum):
    PENDING = "PENDING"
    IN_PROGRESS = "IN_PROGRESS"
    COMPLETED = "COMPLETED"

class TodoListService:
    def create_task(
        self,
        title: str,
        description: str = "",
        priority: Priority = Priority.MEDIUM,
        due_date: Optional[datetime] = None,
        tags: Optional[Set[str]] = None,
        parent_id: Optional[str] = None,
    ) -> str:
        """Returns the generated ID of the new task."""
        ...

    def update_status(self, task_id: str, new_status: Status) -> None:
        """Applies the status and propagates up or down as required."""
        ...

    def delete_task(self, task_id: str) -> None:
        """Removes the task and all descendants."""
        ...

    def query_tasks(
        self,
        status: Optional[Status] = None,
        tag: Optional[str] = None,
        priority: Optional[Priority] = None,
        sort_by: Optional[str] = None,   # "due_date" | "priority"
    ) -> List["Task"]:
        ...

Behaviour to verify

service = TodoListService()
epic_id = service.create_task("Ship v2", priority=Priority.HIGH)
task_id = service.create_task("Write changelog", parent_id=epic_id)

# Completing the parent cascades downward
service.update_status(epic_id, Status.COMPLETED)
assert service.get_task(task_id).status == Status.COMPLETED

# Reopening the child bubbles upward
service.update_status(task_id, Status.IN_PROGRESS)
assert service.get_task(epic_id).status == Status.IN_PROGRESS

Edge cases to address

  • A client holds a subtask ID, then the parent is deleted. What happens?
  • A new subtask is added under an already-COMPLETED parent.
  • Sorting results when some tasks have no due_date.
  • Preventing cycles if task reparenting is supported.
  • Deep trees that would overflow Python's default recursion limit.

Hints

Hint 1

The tree structure is a classic Composite: every node — leaf or subtree — exposes the same interface. Store nodes in a flat dict keyed by ID so any traversal can start in O(1), and keep parent pointers so you can walk upward without a separate reverse-index.

Hint 2

Split propagation into two non-overlapping routines: one walks strictly downward (iterative DFS), the other walks strictly upward (follow parent pointers). If they never call each other, there is no ping-pong loop where reopening a child re-completes it.

Hint 3

Model each filter as a small object with a matches(task) method (the Specification pattern). Compose them with AND logic inside query_tasks. Adding a new filter — due-date range, full-text search — then means adding a class, not modifying existing method signatures.