Interview Question: Two Missing Numbers


Coding interview question from

In this video, I show how to find the two numbers missing from a sequence of integers

Do you have a big interview coming up with Google or Facebook? Do you want to ace your coding interviews once and for all? If so, Byte by Byte has everything that you need to go to get your dream job. We’ve helped thousands of students improve their interviewing and we can help you too.

Stuck on Dynamic Programming? Check out our free ebook:

50 Practice Questions:

You can also find me on


Xem thêm bài viết khác:


  1. Using the quadratic equation

    class Test {

    public static void main(String[] args) {
    int n = 5;
    int[] a = {1, 3, 5};
    int sumOfElements = (n * (n + 1) / 2) – getSumOrProduct(a, true);
    int productOfElements = getFactorial(n) / getSumOrProduct(a, false);
    double discriminant = (Math.pow(sumOfElements, 2) – 4 * productOfElements);
    int x = (sumOfElements + (int) Math.sqrt(discriminant)) / 2;
    int y = (sumOfElements – (int) Math.sqrt(discriminant)) / 2;

    private static int getSumOrProduct(int[] a, boolean getSum) {
    int sum = 0;
    int product = 1;
    for (int element : a) {
    sum += element;
    product *= element;
    if (getSum) {
    return sum;
    return product;

    private static int getFactorial(int n) {
    int factorial = 1;
    for (int i = 1; i <= n; i++) {
    factorial *= i;
    return factorial;

  2. How do we know that the missing numbers in [1, 2, 4] are 3 and 5? Couldn't it be 0 and 3? Will the pattern always start with 1?

  3. @ByteByByte Won't the oneMissing solution break down?
    Because 1^2 = 3 (calculating totalXOR) and then when you try to go 3, it is 3^3 which is 0, so you get an incorrect sum?

    Also, I absolutely loved this playlist. It would be great if you could add more videos!!

  4. This implementation will fail on this example: Range [0-5] i.e. A' = [0, 1, 2, 3, 4, 5] and input arr A = [5, 3, 0, 4]. As per implementation in this video pivot = (15 – 12)/2 = 1.

    TotalLeftXOR = 1 (As i starts from 1)
    arrLeftXOR = 5
    TotalRightXOR = 2 ^ 3 ^ 4 ^ 5 = 0
    arrRightXOR = 3 ^ 0 ^ 4 = 7

    {1^5, 0^7} = {4, 7}

    Am I missing something?

  5. Hello Sam. Recently subscribed to your channel. It's really great work man. Thanks a lot. Can you suggest where I can find programming questions like these, please ??

  6. Such a great idea to use XOR here wouldn't have thought about that one, thanks for the video.

  7. Just FYI: I know it's written XOR but I've never heard it pronounced "X OR". Everywhere I've been, we always said "exclusive or". (But we used the abbreviation in writing!)

  8. !!! your solution does Not work when the two elements are at the beginning or at the end.
    sum = numberOfelements *( average)
    sum = numberOfelements* ((min+max)/2) […. min, ….. max]

    when two elements are missing at the end, we have new max = max+2;
    sum = (numberOfelements+2) *( (min+max+2)/2) the average is min+max+2 (missing elements at the end)
    sum = (numberOfelements+2) *((min-2 + max)/2) the average is min-2+max (missing elts at the beginning)


  9. hey can b done in o(n) . we know a+b we can find a2+b2 using summation of first n sq numbers then solve the 2 eq

  10. I think it is enough if we compute one xor i.e either left one or right one. Then we can simply subtract it from (totalSum-arrSum). I think it is a little more efficient that way(But not significantly).

  11. there is a more easy solution than this
    using set_difference method in c++ would find out the ans and that could also find n number of missing characters in the array !!!

  12. I think one solution to this would be similar to your Find All Duplicated one. Only caveat being that either data is signed type or n < MAX/2 which would allow to add n to current value. Array size is not issue since due to required size is Source + Answer in length so can use answer part to make up. Just initialize them equal to first element of source array to avoid complications.

  13. How to solve problem if given array is {3, 4, 5, 6} and missing numbers are 1, 2 ?
    in this case, both missing nos are on left side of pivot.

  14. Excellent video, but a few points about the solution:
    1. Here you incorrectly assume the array is already sorted. The problem statement doesn't specify that.
    2. You don't have to calculate the second missing element, just subtract the (TotalSum – arrSum)-(firstMissingElement).

  15. Thanks a lot men it's so helpful I can do it now can you sub me please and thanks if you sub me then I will sub you oke thanks so much if you do ttttttt

  16. Great job! Please, can you make a tutorial explaining problem 16.3 of Cracking the Coding Interview. You already made a tutorial explaining a similar problem, but I think it would be good to explain that one, too. The problem says: Given two straight line segments (represented as a start point and an end point), compute the point of intersection, if any. Thank you.

  17. It seems like you could use this technique for k missing numbers by calculating xor overlaps on k sections but at some point you would exceed O(n) time.

  18. First off, thanks for making these videos, there aren't enough coding interview videos out there on YouTube. I'm considering making some of my own one day. Good job!
    Secondly, as a software engineer to another software engineer, I have a interview question I'd like you to solve in one of your videos. I think it would be a great one for the viewers.
    You are building a binary search tree from scratch. In your design, you are to implement a method to find a random node in that tree where each node has equal chance of being selected.


Please enter your comment!
Please enter your name here