Merge Ranked Results From Two Search Shards Without Duplicates
Last updated: November 29, 2026
Quick Overview
Two index shards each return a list of (url, score) pairs sorted by score descending. Merge them into the global top k results where each URL appears once, keeping its highest score.
Yandex
November 29, 20260
0
0 solved
Two index shards each return a list of (url, score) pairs sorted by score descending. Merge them into the global top k results where each URL appears once, keeping its highest score.
Yandex Search fans a query out to many index shards and a meta-searcher merges their partial top lists, so this is a natural warm-up in an early coding screen. The interviewer checks that you use the sorted order instead of concatenating and re-sorting, and that duplicate handling is correct.
What the Interviewer Expects
- Use a two-pointer merge that exploits the descending order
- Skip a URL already emitted, since its first appearance has the higher score
- Stop as soon as k results are collected
- Handle empty or short shard lists
- State O(k + duplicates) time rather than O(n log n)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How does the merge change with m shards instead of two?
- What if the same URL can have different scores because shards use different ranking models?
- How would you merge when each shard can only stream results lazily?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: lists a and b, each sorted by score descending, and an integer k. Output: up to k unique URLs in descending score order. If a URL appears in both shards (replicated documents), keep the...
High-Level Design
This is the merge step of merge sort with a filter. Because both lists are descending, the next element of the global order is always the larger of the two heads. The first time we see a URL is theref...