Relative Sort Array

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 arr1 and arr2. The values in arr2 are distinct, and every value of arr2 also occurs in arr1. Reorder arr1 so that values appearing in arr2 come first, grouped together, in the same relative order as in arr2 (all copies of arr2[0], then all copies of arr2[1], and so on). Values that do not appear in arr2 go at the end in ascending order. Return the reordered array.

Examples

Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]
Follow the order 2, 1, 4, 3, 9, 6, then append the leftovers 7 and 19 sorted.
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
Output: [22,28,8,6,17,44]
17 and 44 are not in `arr2`, so they trail in ascending order.

Constraints

  • 1 <= arr1.length, arr2.length <= 1000
  • 0 <= arr1[i], arr2[i] <= 1000
  • All values of `arr2` are distinct and each appears in `arr1`

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