Excuse me, that’s MY SPOT!
4 min readFeb 13, 2021
Every Jack has his Jill.
Every man can expect to have his partner.
Recently, I did some interesting array-related interview questions. The problem description starts like this or similar: Given an array nums
containing n
numbers in the range [0, n]
… and asks you to find duplicate/missing numbers.
Here is one of the problem description
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.For example,Input:
[4,3,2,7,8,2,3,1]Output:
[5,6]
The SO-EASY Solution
One can quickly come up with a solution which uses an additional array to store each of the element at its correct spot.
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
int[] temp = new int[n]; for (var num : nums) {
temp[num - 1] = num;
} List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; ++i) {
if (temp[i] == 0) {
res.add(i + 1);
}
}
return res;
}
}
That’s it! Thanks for reading ;) I’m kidding. This might be the appetizer or warm-up for the interview and your interviewer…