Merge Two Sorted Search Result ID Lists Without Duplicates

Last updated: November 29, 2026

Quick Overview

Two search backends each return a sorted list of document IDs that may contain repeats. Merge them into one sorted list in which every ID appears exactly once.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Two search backends each return a sorted list of document IDs that may contain repeats. Merge them into one sorted list in which every ID appears exactly once.

A typical Google phone-screen warm-up framed around merging posting lists from two index shards before ranking. The interviewer checks that you use the sorted order with two pointers instead of a set plus sort, and that duplicates inside one list and across lists are both handled.

What the Interviewer Expects
  • Uses two pointers and a single linear pass
  • Removes duplicates both within and across lists
  • Compares against the last emitted value instead of a set
  • Handles one list running out cleanly
  • States O(n + m) time and O(1) extra space beyond output
Key Topics to Cover
Two Pointers
Merging
Sorted Arrays
Deduplication
Posting Lists
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you merge k sorted lists from k shards?
  • What if the lists are too large for memory and arrive as streams?
  • How would you compute the intersection instead of the union?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: two lists a and b, each sorted ascending, possibly with repeated IDs.
  • Output: a sorted list containing every ID that appears in either list, each exactly once.
  • Either list may be empt...
High-Level Design

Keep one pointer into each list. At every step take the smaller current value (either one when they tie) and append it only if it differs from the last value written to the output. Advancing only the ...


Submit Your Answer
Markdown supported