Fuse two ranked lists

Fuse two ranked lists

Hugging Face RAG Interview Question

Hybrid search runs a keyword query and a vector query and gets back two ranked lists with no comparable scores. Reciprocal rank fusion combines them using only the positions, counting the top of a list as rank 1.

Write a function fuse(lists, k) that returns every document id ordered by its total score, highest first. Break ties by id in alphabetical order.

Note

score(d)=∑lists holding d1k+rank(d)\text{score}(d) = \sum_{\text{lists holding } d} \frac{1}{k + \text{rank}(d)}

Asked of

  • AI Engineer
  • ML Engineer
  • Data Scientist

Example 1

Input

lists = [["a", "b"], ["b", "c"]], k = 60

Output

["b", "a", "c"]

Example 2

Input

lists = [["x"], ["x"]], k = 60

Output

["x"]

Explanation

With k = 60, "b" scores 1/61 from the first list and 1/61 from the second, which is more than "a" gets from its single first place. A document near the top of both lists beats one that is first in only one.

Submit also runs 2 hidden test cases that check edge cases.

Similar questions