Problem, as an interviewer would state it:
"Implement intersection(a, b) — returning every element common to BOTH arrays, respecting real MULTIPLICITY (an item appearing twice in both should appear twice in the result), and preserving array a's own relative order."
Examples:
intersection([1,2,2,3], [2,2,4]); // [2,2]
Clarifying questions expected:
- If an element appears multiple times in BOTH arrays, does the result need to genuinely reflect that multiplicity, or should it be de-duplicated to a single occurrence?
- Which array's own relative order should the result preserve — array
a's, arrayb's, or does it not matter? - What is the real, correct count when array
ahas fewer copies of an item than arraybdoes (or vice versa)?
Code / implementation expected: Yes — real, direct proof that multiplicity is respected (not collapsed to a set-style intersection), and that array a's own relative order is preserved in the result.