Coding Interview Question: Two Missing Numbers

Two Missing Numbers: Coding Interview Question

Question

Given an array containing all the numbers from 1 to n except two, find the two missing numbers.

eg.

missing([4, 2, 3]) = 1, 5

Once you think that you’ve solved the problem, click below to see the solution.

 

As always, remember that practicing coding interview questions is as much about how you practice as the question itself. Make sure that you give the question a solid go before skipping to the solution. Ideally if you have time, write out the solution first by hand and then only type it into your computer to verify your work once you've verified it manually. To learn more about how to practice, check out this blog post.


Solution

How was that problem? You can check out the solution in the video below.

Here is the source code for the solution shown in the video (Github):

// Determine the single number that is missing. 
// XOR the actual array and the expected array from 1 to N together. All
// the repeated numbers cancel out, leaving us with the desired result.
// (1 ^ 2 ^ ... ^ N-1 ^ N) ^ (1 ^ 2 ^ ... ^ N-1) = N
public static int oneMissing(int[] arr) {
    int totalXor = 0;
    int arrXor = 0;
    
    // XOR the numbers from 1 to N, ie. the input if no numbers were missing
    for (int i = 1; i <= arr.length + 1; i++) totalXor ^= i;
        
    // XOR the input array
    for (int i : arr) arrXor ^= i;
       
    // XOR the two values together. x^x = 0 and x^0 = x. That means that any
    // repeated number cancels out, so we are left with the single 
    // non-repeated number.
    // eg. (1 ^ 2 ^ ... ^ N-1 ^ N) ^ (1 ^ 2 ^ ... ^ N-1) = N
    return totalXor ^ arrXor;
}
    
// Determine the two numbers missing from an array. Returns an array of 
// length 2
public static int[] twoMissing(int[] arr) {
    int size = arr.length + 2;
       
    // 1 + 2 + ... + N-1 + N = N * (N + 1) / 2
    long totalSum = size * (size + 1) / 2;
       
    // Sum up the input array
    long arrSum = 0;
    for (int i : arr) arrSum += i;
        
    // totalSum - arrSum = the sum of the two results. Therefore we know 
    // that since our two results are not equal, one result is
    // > (sum of two results) / 2 and the other is 
    // < (sum of two results) / 2
    int pivot = (int) ((totalSum - arrSum) / 2);
       
    // Use the same technique as oneMissing() on each half of the array.
    int totalLeftXor = 0;
    int arrLeftXor = 0;
    int totalRightXor = 0;
    int arrRightXor = 0;
        
    for (int i = 1; i <= pivot; i++) totalLeftXor ^= i;
    for (int i = pivot + 1; i <= size; i++) totalRightXor ^= i;
    for (int i : arr) {
        if (i <= pivot) arrLeftXor ^= i;
        else arrRightXor ^= i;
    }
        
    return new int[]{totalLeftXor ^ arrLeftXor, totalRightXor ^ arrRightXor};
}

Did you get the right answer to this coding interview question? Please share your thoughts in the comments below.

Don't do another coding interview...​

…Until you’ve mastered these 50 questions!

50 Coding Interview Questions Cover
Sam Gavis-Hughson

Sam Gavis-Hughson

Sam, founder of Byte by Byte, helps software engineers successfully interview for jobs at top tech companies. Sam has helped thousands of students through his blog and free content -- as well as 400+ paying students -- land jobs at companies such as Google, Amazon, Microsoft, Bloomberg, Uber, and more.