Sorted Unique Intersection of Two NumPy Arrays

Quick Overview

Return the values two 1-D NumPy arrays share, with duplicates removed and sorted ascending, and an empty array when nothing is shared. It tests NumPy set routines, deduplication, sort cost and the assumptions behind faster variants for inputs that are already sorted or unique.

Sorted Unique Intersection of Two NumPy Arrays

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given 1-D NumPy arrays `a` and `b` (possibly with duplicates, in any order), return a NumPy array of the values present in both, unique and sorted ascending. An empty intersection yields an empty array. Example: ```python a = np.array([3, 1, 2, 2, 4]) b = np.array([2, 4, 4, 5]) # Output: array([2, 4]) ``` ```hint Set operations NumPy has set routines for 1-D arrays. Check what they guarantee about order and duplicates. If you build it by hand, deduplicate each input first and look at what a shared value looks like once both inputs are combined and sorted. ``` ### What a Strong Answer Covers - Removing duplicates within each input so every shared value appears once. - Producing ascending output and an empty array when nothing is shared. - Time and space cost in terms of the two input lengths. - Pitfalls such as filtering with a membership mask, which keeps duplicates and input order. ### Follow-up Questions - How would you also return the indices of the common values in each input? - If both inputs are already sorted and unique, what work can you skip, and what goes wrong if that assumption is false? - How would you keep duplicates, returning each common value as many times as it appears in both arrays (the smaller of its two counts)?

Overview: Return the values two 1-D NumPy arrays share, with duplicates removed and sorted ascending, and an empty array when nothing is shared. It tests NumPy set routines, deduplication, sort cost and the assumptions behind faster variants for inputs that are already sorted or unique.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given 1-D NumPy arrays a and b (possibly with duplicates, in any order), return a NumPy array of the values present in both, unique and sorted ascending. An empty intersection yields an empty array.

Example:

a = np.array([3, 1, 2, 2, 4])
b = np.array([2, 4, 4, 5])

# Output: array([2, 4])

What a Strong Answer Covers Guidance

  • Removing duplicates within each input so every shared value appears once.
  • Producing ascending output and an empty array when nothing is shared.
  • Time and space cost in terms of the two input lengths.
  • Pitfalls such as filtering with a membership mask, which keeps duplicates and input order.

Follow-up Questions Guidance

  • How would you also return the indices of the common values in each input?
  • If both inputs are already sorted and unique, what work can you skip, and what goes wrong if that assumption is false?
  • How would you keep duplicates, returning each common value as many times as it appears in both arrays (the smaller of its two counts)?