Skip to the solution
HardDSAAsked in 20247 min read

Find the Intersection of N Arrays (Not Just Two)

716 views

The problem

Problem, as an interviewer would state it: "Return the values that appear in ALL of N arrays, not just two. Discuss edge cases, runtime, and how the arrays' sizes and order affect performance."

Examples:

Code
intersectAll([1, 2, 3, 4], [2, 4, 6], [4, 2, 9]); // [2, 4]
intersectAll([4, 2, 1], [1, 2, 4]);               // [4, 2, 1]  (order of the first array)

Clarifying questions expected:

  • Should the result contain each value once (set semantics), or respect counts (multiset)?
  • Which array's order should the result follow?
  • What should zero arguments, one array, or an empty array among the inputs return?

Code / implementation expected: Yes — real, direct proof against a brute-force reference, plus a measurement of how ordering and early exit change the cost.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: N-array intersection matches brute force, respects the first array's order, never double counts duplicates, and exits early

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 147 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

Implement an LFU (Least Frequently Used) Cache

Continue