Workspace/Coding labs
Loading progress

Select scoped memory records

Intermediate50 min

Implement select_memory(records, user, now, limit). Each record has id, scope (* or a user), at, expires (integer or None), tier, and key. Exclude other users, future records, and records whose exclusive expiry has arrived. For each (tier,key), prefer the user's scope over global scope, then the latest timestamp, then the lexicographically greatest identifier. Sort winners by descending timestamp and ascending identifier; return at most limit. Raise ValueError for a negative limit. Inputs otherwise satisfy this schema. This is a memory selection algorithm, not an identity provider.

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

InputTwo records with the same key: global at 4, personal at 2; now=5

OutputThe personal record

Specific scope wins before timestamp.
EXAMPLE 2

InputOne record with expires=5; now=5

Output[]

Expiry is an exclusive upper bound.
solution.pyPython 3.12