The Intersection of Two Arrays interview question asks you to find the common elements between two arrays. Each element in the result must be unique, and you can return the result in any order.
This is a high-frequency question at Microsoft, Amazon, and Adobe. It evaluations your understanding of basic set theory and Hash Table interview patterns. It’s a test of choosing the most efficient way to check for membership.
This problem follows the Hash Set pattern.
nums1 = [1, 2, 2, 1], nums2 = [2, 2]
set1 = {1, 2}.nums2:
set1. Result: [2]. Remove 2 from set1.set1 (already removed).
Result: [2].[2, 2] instead of [2]. The result must contain unique values.sort() when a Hash Set could solve the problem in linear time.Be ready to discuss the trade-offs between a Hash Set ( time, space) and Sorting + Two Pointers ( time, space). Interviewers love when you can provide the space-optimal solution if asked. This is a core Array interview pattern competency.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Intersection of Two Arrays II | Easy | Solve | |
| Check If N and Its Double Exist | Easy | Solve | |
| K-diff Pairs in an Array | Medium | Solve | |
| Count Pairs Whose Sum is Less than Target | Easy | Solve | |
| Find the Distance Value Between Two Arrays | Easy | Solve |