KatInterview

Dynamic Hierarchy Manager with Timestamp Ordering

Data Structure DesignModel answer
MediumModel answerData Structure Designtreesgraphsstate-management

Last updated

Build a system that processes timestamped parent-child updates arriving in any order, maintains a valid tree structure, and prevents cycles.

Problem

You're implementing a workflow tracking system that receives updates about task dependencies. Updates arrive as events, each containing:

  • step_id: unique identifier for a task
  • parent_id: the task this one depends on (or None for root tasks)
  • timestamp: when this update was generated
  • status: current state of the task

The challenge: Events can arrive in any order. A child task's update might arrive before its parent even exists. Later events might carry older timestamps. You must maintain the correct hierarchy based on timestamp ordering.

Your Task

Implement a ProgressTracker class with two methods:

class ProgressTracker:
    def update(self, event: dict) -> None:
        """Process a single event and update internal state."""
        pass
    
    def get_tree(self) -> dict:
        """Return the current hierarchy as a nested dictionary."""
        pass

Rules

  1. Timestamp precedence: For each step_id, only the event with the highest timestamp determines its current state. Ignore events with older timestamps for steps you've already seen.

  2. Hierarchy constraints:

    • Each step has at most one parent
    • If a step references a parent that doesn't exist yet, treat it as a temporary root until the parent arrives
    • When the parent arrives, automatically attach waiting children
  3. Cycle prevention: If assigning a parent would create a cycle (including self-reference), reject that parent assignment but still apply the status and timestamp from the event.

  4. Output format: Return a nested dictionary where each key is a step_id and its value is a dictionary of its children. Leaf nodes map to empty dictionaries {}.

Example

tracker = ProgressTracker()
tracker.update({"step_id": "B", "parent_id": "A", "status": "running", "timestamp": 10})
tracker.update({"step_id": "A", "parent_id": None, "status": "done", "timestamp": 5})
print(tracker.get_tree())
# {"A": {"B": {}}}

tracker.update({"step_id": "C", "parent_id": "B", "status": "pending", "timestamp": 15})
print(tracker.get_tree())
# {"A": {"B": {"C": {}}}}

Edge Cases to Handle

  • Self-referential parent assignments
  • Parent that never appears in any event
  • Reparenting a step (changing its parent via a newer timestamp)
  • Events with equal timestamps (implementation choice: you decide tie-breaking)
  • Deep hierarchies (avoid recursion limits)

Hints

Hint 1

Track both the latest timestamp per step and the effective parent separately - these serve different purposes

Hint 2

For cycle detection, walk ancestors upward from the proposed parent - if you reach the step being moved, you've found a cycle

Hint 3

Maintain a registry of steps waiting for parents that haven't arrived yet, indexed both by the missing parent and by the waiting child for O(1) lookups