Skip to main content

Combine two ranked lists of document ids, one from keyword search and one from vector search, into one list with reciprocal rank fusion. For example, the lists a, b, c and b, d, a with the constant 60 should give the order b, a, d, c.

Coding rounds · Intermediate 2

Short answer

For each list, give every document 1 / (k + rank), with rank starting at 1 and k usually 60. Add the scores for each document across lists in a dictionary, then sort by total score, highest first. A document missing from a list gets nothing from it. Cost is O(total items) plus the sort.

Deeper answer

Clarify first: each input is a list of document ids ordered best first, the lists may have different lengths, and a document may appear in only one.

The steps: keep a dictionary of scores. For each list, loop with for rank, doc in enumerate(results, start=1) and add 1 / (k + rank) to the score of that document. Then sort the ids by score in descending order, with the id as a second sort key so ties are stable. In the example, b gets 1/62 + 1/61 and a gets 1/61 + 1/63, so b scores slightly higher.

Say why this method is used: it needs only ranks, so there is no need to put BM25 scores and cosine scores on one scale. The constant k reduces the weight of the very top ranks; a small k lets the first result of one list dominate. Test a document that is in one list only. Interviewers watch for ranks that start at 0, and for adding raw scores from the two systems.

What a strong answer covers

Answer it out loud in the mock interview about 2.5 min

Follow-ups to expect

Common mistakes

Learn it

Hybrid search, reranking and filters

Practice Coding rounds questions

More on this topic

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.