Workspace/Coding labs
Loading progress

Retrieve scoped and fresh memories

Intermediate65 min

Implement retrieve_memory(records, query_tags, scope, now, max_age, k). Records contain unique string IDs, scope, nonnegative integer time, and a list of tags. Return up to k eligible IDs ranked by 2*overlap + 1/(1+age), breaking ties by ID. Overlap counts distinct shared tags. Exclude wrong scope, future timestamps, expired records, and zero-overlap records before ranking. Validate nonnegative exact integer time controls and k, and reject duplicate IDs. Inputs are trusted well-shaped records except for those explicitly validated conditions.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

InputRecords A(time=10,tags=[p]), B(time=18,tags=[p]), now=20, scope=u, max_age=20, k=2

Output["B","A"]

Equal relevance favors fresher evidence.
EXAMPLE 2

InputThe same records with max_age=1

Output[]

Both records are too old.
solution.pyPython 3.12