Intersection of Two Arrays

EasyArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given two integer arrays nums1 and nums2. Return every value that appears in **both** arrays. Each shared value must appear exactly once in the result, no matter how many times it occurs in either input. Return the values sorted in **ascending** order; if nothing is shared, return an empty array.

Examples

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Only 2 is present in both arrays, and it is listed once.
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
4 and 9 are shared; they are returned in ascending order.

Constraints

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems