All problems
MediumRetrieval & Rankingv2026-06-30

Retrieval & Ranking · Medium

Reranker Interface

Preserve candidate identity while fusing and ranking aligned score batches.

Task

Implement reranker_interface(candidate_ids: list[list[str]], retrieval_scores: list[list[float]], reranker_scores: list[list[float]], k: int, alpha: float) -> tuple[list[list[str]], list[list[float]]]. Rerank each batch of identified candidates with aligned reranker scores and return a deterministic top-k result.

Requirements

  • candidate_ids, retrieval_scores, and reranker_scores have the same number of batches, and their three rows have equal length within every batch.
  • Candidate IDs are strings and are unique within each batch; preserve these IDs exactly rather than replacing them with row positions.
  • All scores are finite numeric values, k is a non-negative integer, and alpha is a finite number in the closed interval [0, 1].
  • For candidate position i, compute final_score[i] = (1 - alpha) * retrieval_score[i] + alpha * reranker_score[i]. Keep all three values aligned by position.
  • Rank each batch independently by descending final score.
  • When final scores tie, preserve the candidates' original input order; this stability rule also determines which candidate survives a tie at the top-k boundary.
  • Return a pair (ranked_ids, ranked_scores) with matching batch and rank positions.
  • Return at most min(k, batch_size) candidates per batch. For k=0, return one empty row in each output per input batch.
  • An empty candidate batch produces an empty output row, and empty outer inputs produce two empty outer lists.
  • Do not mutate any input. The function reranks only the supplied candidates; it does not retrieve, add, deduplicate, or score new candidates.

Example

reranker_interface(
    [["doc-a", "doc-b", "doc-c"]],
    [[0.9, 0.8, 0.7]],
    [[0.2, 0.9, 0.8]],
    2,
    0.75,
)
# ([["doc-b", "doc-c"]], [[0.875, 0.775]])
solution.pySign in to save
Public tests run locally in your browser.
Run your code to see public test results.