Loading...
Loading...
You are building a long-running autonomous AI agent that continuously processes tasks and accumulates memories (key-value observations). Because the agent runs indefinitely, it must manage memory efficiently: it should retain the most recently accessed memories while evicting the least recently used ones when capacity is full. Additionally, each memory has a priority score — when two memories have the same recency, the one with the lower priority score is evicted first.
Implement an AgentMemoryManager class that supports the following operations:
store(key, value, priority) — Store a memory with the given key, value, and integer priority. If the key already exists, update its value and priority, and mark it as most recently used. If the memory is full, evict the least recently used memory (ties broken by lowest priority).recall(key) — Retrieve the value for the given key. If it exists, mark it as most recently used and return the value. Return -1 if the key does not exist.forget(key) — Explicitly remove a memory by key. Return True if removed, False if not found.snapshot() — Return a list of all current keys in order from most recently used to least recently used.You will receive a list of operations and their arguments. Return the list of results for each operation (use null for store operations, and the appropriate return value for recall, forget, and snapshot).
Function signature:
def agentMemoryManager(capacity: int, operations: List[List]) -> List
Input: capacity = 2, operations = [["store","task1","data1",1],["store","task2","data2",2],["recall","task1"],["store","task3","data3",1],["recall","task2"],["snapshot"]]
Output: [null, null, "data1", null, -1, ["task3", "task1"]]
Explanation:
store("task1","data1",1) → memory: store("task2","data2",2) → memory: (task2 most recent)recall("task1") → returns "data1", task1 becomes most recentstore("task3","data3",1) → capacity full; task2 is LRU → evict task2; memory: 1 <= capacity <= 1000 1 <= number of operations <= 500 1 <= priority <= 100 Keys are non-empty strings of length 1-20 Values are non-empty strings of length 1-50
recall("task2") → returns -1 (evicted)snapshot() → ["task3", "task1"]Input: capacity = 1, operations = [["store","a","alpha",5],["store","b","beta",3],["recall","a"],["snapshot"]]
Output: [null, null, -1, ["b"]]
Explanation: Capacity is 1. Storing "b" evicts "a". Recalling "a" returns -1.
Input: capacity = 3, operations = [["store","x","10",2],["store","y","20",1],["store","z","30",3],["forget","y"],["store","w","40",1],["snapshot"]]
Output: [null, null, null, true, null, ["w", "z", "x"]]
Explanation: After storing x, y, z (full), forget y frees a slot. Storing w succeeds without eviction. Snapshot reflects recency order.