Intersection of Two Arrays II
A easy-tier problem at 59% community acceptance, tagged with Array, Hash Table, Two Pointers. Reported in interviews at Yandex and 0 others.
Intersection of Two Arrays II is one of those problems that seems trivial until you hit the edge cases in a live OA. You're given two arrays and need to return their intersection, accounting for duplicates. At 59% acceptance, candidates often overthink the implementation or nail the obvious approach but fail on follow-ups about space complexity and multiple passes. Yandex has asked this. It's easy, but easy problems hide tricks: the interviewer is testing whether you think past the first solution and how you handle memory constraints. If you blank on the optimal path during your assessment, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Intersection of Two Arrays II"
Intersection of Two Arrays II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe naive approach is a hash map or frequency counter: drop one array into a map, iterate the second, and collect matches. That gets you passing in most cases. The gotcha is follow-ups. Can you do it with O(1) extra space if the input is sorted. Can you handle the case where one array is much smaller. Binary search on a sorted array cuts space to nearly nothing. Two pointers, if both are pre-sorted, is slick and intuitive. Most candidates lock into hash table and never pivot, even when the interviewer hints at space. The problem tests whether you can reason about tradeoffs: time vs. memory, single-pass vs. multi-pass, and when sorting the input upfront is faster than building a map. In a live OA, if your first solution works, pause and ask yourself whether it scales if one array is a million elements. StealthCoder the hedge here is that you've already seen the two-pointer and binary search angles, so you don't waste five minutes re-deriving them under pressure.
Pattern tags
You know the problem.
Make sure you actually pass it.
Intersection of Two Arrays II recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Intersection of Two Arrays II interview FAQ
How hard is this really at an actual company interview?+
At 59% acceptance, half of candidates miss something. Most pass the basic test case but fail when arrays have duplicates or when the interviewer asks for O(1) space on sorted input. It's easy by topic, medium by execution under pressure. You need to be ready for the second part before you're done with the first.
What's the trick everyone misses?+
Duplicates. If input is [1, 2, 2, 1] and [2, 2], the output must be [2, 2]. A simple set-based approach loses count. Use a frequency map or sort both arrays and walk them in parallel. The two-pointer method is clean if both inputs are already sorted.
Do I have to sort the input first?+
No, but sorting is sometimes faster than building a hash table, especially if one array is much larger. If both are sorted upfront, two pointers is O(n + m) time and O(1) space (minus the output array). Hash map is faster if arrays arrive unsorted and you don't want to touch the inputs.
Is this still asked at real companies?+
Yes. Yandex has asked it. It appears in screening rounds as a warm-up problem, often before harder array or hash table problems. If you see it in a real OA, it's testing your baseline reasoning, not a trap.
How does this relate to other hash table or array problems?+
It's a foundation for set operations and intersection logic. Once you own this, you're ready for more complex multi-array or interval problems. The pattern of 'map one input, iterate the other' repeats constantly in medium and hard problems.
Want the actual problem statement? View "Intersection of Two Arrays II" on LeetCode →