Hierarchical Memory System

Hard
Agents

Implement hierarchical three-tier memory:

Tiers:

  1. Working Memory: Size 5, immediate, critical items
  2. Short-Term Memory: Size 50, recent context
  3. Long-Term Memory: Vector store, 10K capacity

Operations:

  1. add(item, priority): Add to appropriate tier
    • 'critical' → working
    • 'normal' → stm
  2. promote(item_id, to_tier): Move up hierarchy
  3. retrieve(query, max_tokens): Fill context window
    • Take all from working
    • Fill remainder from stm
    • If still space, query ltm
  4. consolidate(): Move old items down tiers
  5. estimate_tokens(items): Count tokens

Eviction:

  • Working full → oldest to stm
  • Stm full → oldest to ltm
  • Ltm full → vector eviction

Examples

Example 1:
Input: hm = HierarchicalMemory(working_size=2); hm.add('critical info', 'critical'); len(hm.working)
Output: 1
Explanation: Critical item added to working memory

Starter Code

class HierarchicalMemory:
    """
    Three-tier hierarchical memory: Working -> Short-term -> Long-term
    """
    
    def __init__(self, working_size=5, stm_size=50, ltm_capacity=10000):
        self.working_size = working_size
        self.stm_size = stm_size
        self.ltm_capacity = ltm_capacity
        
        self.working = []  # Highest priority, immediate access
        self.stm = []      # Recent context
        self.ltm = None    # Vector store for long-term
        
        self.promotion_stats = {'stm_to_working': 0, 'ltm_to_stm': 0}
    
    def add(self, item, priority='normal'):
        """
        Add item to appropriate memory tier based on priority.
        Priority: 'critical' -> working, 'normal' -> stm
        """
        # Your implementation here
        pass
    
    def promote(self, item_id, to_tier):
        """Promote item to higher tier (stm->working, ltm->stm)"""
        # Your implementation here
        pass
    
    def retrieve(self, query, max_tokens=1000):
        """
        Retrieve context filling up to max_tokens.
        Priority: working > stm > ltm
        """
        # Your implementation here
        pass
    
    def consolidate(self):
        """
        Move old working items to stm, old stm to ltm.
        Simulates sleep/consolidation.
        """
        # Your implementation here
        pass
    
    def estimate_tokens(self, items):
        """Estimate token count for items"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews