Find Intersection
Python data engineering interview problem. Difficulty: beginner. Pattern: Hashing. About 10 minutes. Free to practice.
Return the sorted unique intersection of two lists. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Suppose you have two lists of customer ids, and you want to know which customers appear in both lists. This is called the intersection of the two lists. Write a function list_intersection(a, b) that takes two lists and returns a new list with the values that are present in both. Each value should appear only once, and the result must be sorted from smallest to largest. For [1, 2, 2, 1] and [2, 2] the answer is [2]. Even though 2 appears twice in each list, it is listed once. Keep the print lines in the editor.
Requirements
- Return only values that appear in both a and b.
- Each value must appear once in the result, even if it is repeated in the inputs.
- Sort the result from smallest to largest.
- Return [] if the two lists have nothing in common.
Constraints
- The values can be used in a set, for example numbers.
- The lists can be empty and can contain repeated values.
Examples
Input: list_intersection([1, 2, 2, 1], [2, 2]) Output: [2] Only 2 is shared.
Topics: lakebench, python, set, intersection.
More Python interview questions · All interview problems · Learn data engineering
Find Intersection
Interview-style drill: Return the sorted unique intersection of two lists.
Implement `list_intersection(a: list, b: list) -> list` returning sorted unique values present in both. Example: `[1, 2, 2, 1], [2, 2]` → `[2]`. Keep the harness.