Implement Short-Term vs Long-Term Memory

Easy
Agents

Agent Memory Systems

Agents use memory to maintain context and persist knowledge across sessions.

Task

Implement both memory types:

  • ShortTermMemory: Sliding window of recent messages (bounded size).
  • LongTermMemory: Persistent key-value store with keyword search.

Constraints

  • Short-term memory evicts oldest entries when full.
  • Long-term memory persists indefinitely.
  • search() returns all entries whose string representation contains the query.

Examples

Example 1:
Input: stm = ShortTermMemory(max_size=2) stm.add({'role': 'user', 'content': 'Hi'}) stm.add({'role': 'assistant', 'content': 'Hello'}) stm.add({'role': 'user', 'content': 'Bye'}) stm.get_all()
Output: [{'role': 'assistant', 'content': 'Hello'}, {'role': 'user', 'content': 'Bye'}]
Explanation: Oldest entry evicted when max_size exceeded.

Starter Code

from typing import List, Dict, Any

class ShortTermMemory:
    def __init__(self, max_size: int = 10):
        # TODO: Implement sliding window memory
        pass

    def add(self, entry: Dict[str, Any]) -> None:
        pass

    def get_all(self) -> List[Dict[str, Any]]:
        pass

class LongTermMemory:
    def __init__(self):
        # TODO: Implement persistent key-value store
        pass

    def store(self, key: str, value: Any) -> None:
        pass

    def retrieve(self, key: str) -> Any:
        pass

    def search(self, query: str) -> List[Any]:
        # Simple keyword search
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews