The Overlap Finder

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3
Asked in
BCG Securities

Problem

Given two lists of integers, return a sorted list of the distinct values that appear in BOTH.

Two guest lists - who made it onto both?

Example 1

Input:

list1 = [1,2,3,4], list2 = [3,4,5,6]

Output:

[3,4]

Example 2

Input:

list1 = [1,2], list2 = [3,4]

Output:

[]

Worked solution and explanation

Why this problem exists in real interviews

This tests set-based intersection and deduplication, the same core pattern as data reconciliation. It verifies that candidates can find common values between two datasets efficiently.


Break down the requirements

Step 1: Convert one list to a set

This enables O(1) lookups when checking elements from the other list.

Step 2: Find common values

Scan the second list and collect elements present in the set, tracking which have already been added to avoid duplicates.

Step 3: Sort and return

Return the common values in sorted order.


The solution

Set intersection with dedup and sort
def find_overlap(list1, list2):
    set1 = set(list1)
    found = set()
    result = []
    for val in list2:
        if val in set1 and val not in found:
            result.append(val)
            found.add(val)
    result.sort()
    return result

Common follow-up questions