Hierarchical Task Decomposer

Medium
Agents

Implement hierarchical task decomposition:

  1. decompose(task, max_depth): Recursively break down tasks
    • If atomic or max_depth reached, return as leaf
    • Otherwise split and recurse on subtasks
  2. _is_atomic(task): Check if task is indivisible
    • Atomic if: no 'and'/'then', or length < 10 chars
  3. _split_task(task): Split compound task
    • Split on ' and ', ' then ', ','
  4. get_execution_order(): Topological sort of all subtasks

Tree Structure: {'task': str, 'subtasks': [...], 'depth': int, 'id': str}

Execution Order: Return list of task IDs in dependency-respecting order.

Examples

Example 1:
Input: td = TaskDecomposer(); tree = td.decompose('Research and write', 2); 'subtasks' in tree
Output: True
Explanation: Task decomposed into subtasks

Starter Code

class TaskDecomposer:
    """
    Decompose complex tasks into subtasks with dependencies.
    """
    
    def __init__(self):
        self.subtasks = []
        self.dependencies = {}
    
    def decompose(self, task, max_depth=3):
        """
        Recursively decompose task into subtasks.
        Returns tree structure of subtasks.
        """
        # Your implementation here
        pass
    
    def _is_atomic(self, task):
        """Check if task cannot be decomposed further"""
        # Your implementation here
        pass
    
    def _split_task(self, task):
        """Split task into subtasks (simulated)"""
        # Simulate: split by 'and', 'then'
        pass
    
    def get_execution_order(self):
        """Get topologically sorted execution order"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews