Recency and Relevance Memory Scoring
Problem Statement
Rank agent memories by a weighted blend of relevance (similarity to the query) and recency (how recently the memory was accessed), returning the top-k memory ids.
Background
A common retrieval score is score = alpha * relevance + (1 - alpha) * recency, where both components are in [0, 1]. Recency is derived from an age: recency = 1 / (1 + age) so newer (smaller age) memories score higher. Ties are broken by smaller id for determinism.
Your Task
Implement:
def rank_memories(memories, alpha, k):
- memories: list of {"id": int, "relevance": float, "age": float}.
- Compute score = alpharelevance + (1-alpha)(1/(1+age)).
- Return the ids of the top k by score (desc), ties broken by smaller id.
Input Format
- memories (list of dicts), alpha (float in [0,1]), k (int).
Output Format
- A list of ints (memory ids).
Sample
mem = [{"id":1,"relevance":0.9,"age":10},{"id":2,"relevance":0.4,"age":0}]
print(rank_memories(mem, 0.5, 1))
Output:
[2]
Example:
mem = [{"id":1,"relevance":0.9,"age":10},{"id":2,"relevance":0.4,"age":0}]
print(rank_memories(mem, 0.5, 1))[2]
- Calculate the recency component for each memory using the formula recency=1+age1​:
- For Memory 1 (age 10): 1+101​=111​≈0.0909
- For Memory 2 (age 0): 1+01​=1.0
- Compute the total score for each memory using the weighted blend score=α⋅relevance+(1−α)⋅recency with α=0.5:
- Memory 1: 0.5⋅0.9+0.5⋅0.0909=0.45+0.04545≈0.4955
- Memory 2: 0.5â‹…0.4+0.5â‹…1.0=0.2+0.5=0.7
- Rank the memories by score in descending order to determine the top candidates:
- Memory 2 has a score of 0.7
- Memory 1 has a score of ≈0.4955
- Since 0.7>0.4955, Memory 2 is ranked first.
- Select the top k memories where k=1, which corresponds to the single highest-ranked item, Memory 2.
- The final output is [2]
Constraints:
0 <= alpha <= 1,age >= 0,0 <= relevance <= 1.recency = 1/(1+age).- Sort by score desc, then id asc; return the first
kids.
1. Background Knowledge
This problem models a hybrid retrieval score commonly used in AI agent memory systems. Agents often need to recall past experiences, but purely semantic similarity (relevance) can surface outdated facts, while purely temporal ordering (recency) ignores topical fit. The linear combination score=α⋅relevance+(1−α)⋅recency lets a developer tune the trade-off: α=1 ignores time, while α=0 ignores content. Both components are normalized to [0,1] so the weighted sum remains in the same range, making scores comparable across queries.
The recency term uses a simple inverse-age decay: recency=1+age1​. This function is monotonically decreasing in age, so a memory accessed 0 steps ago scores 1.0, one accessed 1 step ago scores 0.5, and so on. The +1 in the denominator avoids division by zero and ensures the value stays strictly positive. This is a lightweight alternative to exponential decay (e−λt) and is sufficient when exact decay rates are not critical.
Finally, the problem requires deterministic tie-breaking: if two memories have identical scores, the one with the smaller id wins. This is a standard pattern in ranking systems to ensure reproducible outputs, which is essential for testing and debugging agent behavior.
2. Algorithm Approach
This is a score-and-sort pattern:
- Compute a scalar score for each memory using the given formula.
- Sort the memories by score in descending order, with a secondary sort key of ascending id for tie-breaking.
- Extract the top k ids from the sorted list.
No data structure beyond a list is needed. The key insight is that Python's sorted() (or list.sort()) supports a custom key function that can return a tuple, enabling multi-criteria sorting in a single pass.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.