Blackboard Knowledge System

Medium
Agents

Implement Blackboard architecture for multi-agent coordination:

Layers (bottom-up):

  1. raw_data: Input data
  2. features: Extracted features
  3. hypotheses: Partial interpretations
  4. solutions: Final answers

Operations:

  1. write(layer, key, value, agent_id): Write to layer
    • Fails if key locked by different agent
    • Record contributor
  2. read(layer, key): Read value
  3. lock/unlock(key, agent_id): Exclusive access control
  4. get_solution(): Return solutions layer if non-empty

Locking:

  • Only lock owner can write/unlock
  • Attempt to write locked key returns False

Examples

Example 1:
Input: bb = Blackboard(); bb.write('raw_data', 'input', 'data', 'agent1'); bb.read('raw_data', 'input')
Output: 'data'
Explanation: Write then read returns the value

Starter Code

class Blackboard:
    """
    Blackboard architecture for multi-agent problem solving.
    Shared workspace where agents read/write partial solutions.
    """
    
    def __init__(self):
        self.data = {}  # Key-value store
        self.layers = {
            'raw_data': {},
            'features': {},
            'hypotheses': {},
            'solutions': {}
        }
        self.contributors = {}  # Track which agent wrote what
        self.locks = {}  # Simple locking mechanism
    
    def write(self, layer, key, value, agent_id):
        """
        Write to blackboard layer.
        Returns True if successful, False if key locked by another agent.
        """
        # Your implementation here
        pass
    
    def read(self, layer, key):
        """Read from blackboard layer"""
        # Your implementation here
        pass
    
    def lock(self, key, agent_id):
        """Lock key for exclusive access"""
        # Your implementation here
        pass
    
    def unlock(self, key, agent_id):
        """Unlock key (only by owner)"""
        # Your implementation here
        pass
    
    def get_solution(self):
        """Get final solution if complete"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews