Score memories against a query and return top-k matches with deterministic tie-breaking.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement retrieve(memories, query, scorer, top_k).
- memories is a list of {"id": str, "content": str}.
- scorer(query: str, content: str) -> float returns a relevance score.
- Return the top_k memories sorted by score **descending**.
- **Deterministic tie-breaking**: when two memories have the same score,
prefer the one that appeared **first** in the original list (lower index).
- Return a list of {"id": ..., "score": ...} dicts.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
def retrieve(memories, query, scorer, top_k):
scored = []
for idx, m in enumerate(memories):Score, sort with tie-break, slice to top_k
Already have Pro access? Sign in