Questions, common mistakes, and hidden Python answer
Questions: Can private content be selected? Are scores stable under
ties? Should one user dominate the queue? Common mistakes include
ignoring consent, sorting the entire stream when a heap is enough,
using raw transcript content in logs, and optimizing uncertainty
while starving approved rare slices. A production queue should
also require minimum slice support, cap or diversify per-user
contributions, and avoid exposing raw transcripts before sending
examples to reviewers.
from heapq import heappush, heappushpop
from math import isfinite
def rank_review_candidates(rows, k, min_slice_count=20):
if not isinstance(k, int) or k < 0:
raise ValueError("k must be a non-negative integer")
if not isinstance(min_slice_count, int) or min_slice_count <= 0:
raise ValueError("min_slice_count must be a positive integer")
if k == 0:
return []
required = ("uncertainty", "slice_rarity", "regression", "slice_count", "example_id")
heap = []
for row in rows:
if not row.get("consented"):
continue
missing = [name for name in required if name not in row]
if missing:
raise ValueError(f"missing candidate fields: {missing}")
for name in ("uncertainty", "slice_rarity", "regression"):
value = row[name]
if not isinstance(value, (int, float)) or not isfinite(value) or not 0 <= value <= 1:
raise ValueError(f"{name} must be a finite score between 0 and 1")
if not isinstance(row["slice_count"], int) or row["slice_count"] < min_slice_count:
continue
score = (
0.55 * row["uncertainty"]
+ 0.30 * row["slice_rarity"]
+ 0.15 * row["regression"]
)
item = (score, row["example_id"])
if len(heap) < k:
heappush(heap, item)
else:
heappushpop(heap, item)
return [example_id for score, example_id in sorted(heap, reverse=True)]
rows = [
{"example_id": "a", "consented": True, "uncertainty": 0.9, "slice_rarity": 0.2, "regression": 0.3, "slice_count": 80},
{"example_id": "b", "consented": False, "uncertainty": 1.0, "slice_rarity": 1.0, "regression": 1.0, "slice_count": 100},
{"example_id": "c", "consented": True, "uncertainty": 0.5, "slice_rarity": 0.9, "regression": 0.8, "slice_count": 40},
{"example_id": "d", "consented": True, "uncertainty": 0.8, "slice_rarity": 1.0, "regression": 1.0, "slice_count": 3},
]
assert rank_review_candidates(rows, 2) == ["c", "a"]