MediumRetrievalPython 3

Document Permission Filter

Filter retrieval chunks by public, tenant, group, and private permissions before ranking.

30m2 sample tests3 hidden tests

Filter retrieved chunks by user permissions before ranking them.

Requirements

  • Define filter_chunks(chunks, user, k).
  • Each chunk has id, score, visibility, and optional tenant, groups, owner.
  • visibility == "public" is visible to everyone.
  • visibility == "tenant" is visible when chunk tenant matches user tenant.
  • visibility == "group" is visible when tenant matches and at least one group overlaps.
  • visibility == "private" is visible only to the owner.
  • Return selected chunk IDs sorted by score descending, then ID alphabetically.
  • Return at most k IDs.

Example

python
1chunks = [{"id": "a", "score": 1, "visibility": "public"}] 2assert filter_chunks(chunks, {"id": "u", "tenant": "t", "groups": []}, 3) == ["a"]

Constraints

  • Never return unauthorized chunks.
  • Do not mutate input chunks.
  • Missing group lists count as empty.

Editor
Results
Run sample tests or submit all tests.