Set Mismatch

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

An array nums of length n was supposed to contain each integer from 1 to n exactly once. Due to an error, one number was overwritten with a copy of another, so one value now appears **twice** and one value is **missing**. Return a two-element array [duplicate, missing]: first the value that occurs twice, then the value that no longer occurs. The array is not necessarily sorted.

Examples

Input: nums = [1,2,2,4]
Output: [2,3]
2 appears twice and 3 is absent.
Input: nums = [1,1]
Output: [1,2]
1 appears twice and 2 is absent.

Constraints

  • 2 <= nums.length <= 10^4
  • 1 <= nums[i] <= 10^4
  • Exactly one value in `1..n` is duplicated and exactly one is missing

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