Interview Question: Two Missing Numbers

Sdílet
Vložit
  • čas přidán 28. 08. 2024

Komentáře • 75

  • @basheeral-momani2032
    @basheeral-momani2032 Před 3 lety

    I liked your smile when you go from one solution to a better solution

  • @TheSrini007
    @TheSrini007 Před 6 lety +10

    Amazing Solution. Without Memoization, I could not think of considering the half-of-sum idea. Brilliant!

  • @josephluce3801
    @josephluce3801 Před 8 lety +3

    First off, thanks for making these videos, there aren't enough coding interview videos out there on CZcams. 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.

    • @ByteByByte
      @ByteByByte  Před 8 lety +1

      Thanks! :)
      That sounds like an interesting question. Do you know of a better solution than either generating the tree, then assigning numbers to each node and randomly picking one, or doing an online algorithm where you randomly select or reject each node as you go?

    • @josephluce3801
      @josephluce3801 Před 8 lety

      I'll send you an email of the explanation, you'd have to be very careful with your concept to maintain that 'equal chance' probability for all nodes.

  • @chintalapativenkataramarahul

    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).

  • @rajrsa
    @rajrsa Před 5 lety +1

    @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!!

  • @itaycarpis9291
    @itaycarpis9291 Před 3 lety

    XOR is not needed here at all.
    Take a look:
    void TwoMissing(int* arr, int size, int* first, int* second)
    {
    int total = 0;
    int arrTotal = 0;
    for (int i = 1; i

  • @usman9571
    @usman9571 Před 5 lety +2

    Dude, just wanted to say you're an awesome teacher! Thanks for taking out the time to make these videos!

  • @nikhilgoyal8340
    @nikhilgoyal8340 Před 4 lety +2

    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;
    System.out.println(x);
    System.out.println(y);
    }
    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

  • @Dyslexic_Neuron
    @Dyslexic_Neuron Před 5 lety +1

    more of a maths question than programming!! Good Explanation bro :D

  • @HallaBool
    @HallaBool Před 7 lety +3

    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).

    • @ByteByByte
      @ByteByByte  Před 7 lety

      1. What makes you think the array needs to be sorted? This solution shouldn't require that.
      2. In this case how are you calculating firstMissingSum to begin with?

    • @HallaBool
      @HallaBool Před 7 lety +4

      Thanks for your response. To address your points:
      1. The step when you get the pivot index, on the array, once you get the total sum. You look for the missing array element in index lower than the pivot index. This can only be true if the array is sorted.
      2. You get the first missing number by following the step you mention (to get the first missing element in the lower half). But instead of doing the lookup for the upper half for the second missing element. Just take the difference between the first missing element and the totalSum of the missing elements.
      For Example: Lets say the totalSum missing is 8 and the first missing element is 3, then we can just do a 8 - 3 to get the second missing element.

    • @ByteByByte
      @ByteByByte  Před 7 lety +2

      1. So when you're pivoting, you're not actually paying any attention to the order, you're just saying for each element is it greater or less than the pivot. Therefore it's not actually reliant on the order.
      2. Yes you could do that, but that doesn't really seem like any less effort to me

    • @aravindhravichandran5152
      @aravindhravichandran5152 Před 7 lety +1

      Someshwar Chatterjee your question is intuitive ... but if we closely look in the pivot calculation.
      let's say the given array is
      [4 1 5]
      size = size+2
      so size =5
      totalsum = (5*6)/2=15
      arrsum=4+1+5=10
      pivot =(15-10)/2=2
      it means that the value one value will lie between (1,2) and other between (3,5).
      a clear understanding can be with this example
      given array is [3 1 2]
      now totalsum= 15
      arrsum=6
      pivot = 4
      so one value will definitely lite between (1,4) and the other will lie between (5,5) (which is five itself ).. did I make sense?

    • @atishnarlawar1177
      @atishnarlawar1177 Před 6 lety

      Just want to get clarified, as I had the same question. The algorithm calculates xor on the based of comparing pivot value with array elements and not with array's pivot index.

  • @roberthelk6492
    @roberthelk6492 Před 7 lety

    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.

    • @ByteByByte
      @ByteByByte  Před 7 lety

      Interesting, hadn't though of doing it that way, but you're right that would work too. Thanks for sharing!

  • @neelimagupta4921
    @neelimagupta4921 Před 7 lety +9

    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.

    • @ByteByByte
      @ByteByByte  Před 7 lety +14

      totalSum - arrSum / 2 = 3/2 = 1. You still pivot in the same way and end up with 1 on one side and 2^3^4^5^6 on the other.

    • @mohdrasid6737
      @mohdrasid6737 Před 3 lety

      I was also thinking of this case... we are XORing of those numbers which are( less or equal to the pivot) or (greater than the pivot) ...so no matter where these numbers appear in array , only one element come from Left XOR and one from right XOR. ☺

  • @falakk22
    @falakk22 Před 7 lety +2

    Great illustration of problem towards complex form ,Thanks.

  • @dailyclassicwowmoments6395

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

  • @loneshaan8531
    @loneshaan8531 Před 4 lety

    totalSum formulla is wrong , it should be => totalSum = size * (size + 1) / 2. en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF
    UPDATE: he has corrected himself at the end of this video

  • @yuvrajoberoi7834
    @yuvrajoberoi7834 Před 4 lety

    The solution is brilliant man

  • @dheerajnadakatla7118
    @dheerajnadakatla7118 Před 3 lety

    This solution in mind blowing!

  • @swapniljain3459
    @swapniljain3459 Před 7 lety +20

    i thought calculation for a sum of sequence of numbers is [(n)(n+1)/2 ] rather than [(n)(n-1)/2]]

    • @ByteByByte
      @ByteByByte  Před 7 lety +6

      yep you're right; its corrected in the blog post

    • @kimerseen8934
      @kimerseen8934 Před 4 lety +1

      n*(n-1)/2 if sequence starts with 0

    • @swapniljain3459
      @swapniljain3459 Před 4 lety +1

      Kimer Seen yes but sequence of n numbers is always considered as starting from 1 rather than 0.

  • @amellperalta
    @amellperalta Před 8 lety

    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.

    • @ByteByByte
      @ByteByByte  Před 8 lety +1

      Hey thanks for the suggestion! I actually already did a solution to that problem, which you can see here :) www.byte-by-byte.com/lineintersection/

  • @evayang1398
    @evayang1398 Před 4 lety

    It's really inspiring! Thanks for the video!

  • @priyankarrajgupta4198
    @priyankarrajgupta4198 Před 5 lety +2

    Will this solution ends up in a overflow when n is sufficiently large.

  • @pranavkumar9943
    @pranavkumar9943 Před 5 lety

    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 ??

  • @chiamarc
    @chiamarc Před 8 lety

    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.

    • @ByteByByte
      @ByteByByte  Před 8 lety +1

      You can definitely generalize the algorithm, although it gets a bit more complicated. There is some discussion of that here if you're curious stackoverflow.com/questions/3492302/easy-interview-question-got-harder-given-numbers-1-100-find-the-missing-numbe

  • @hamiddoust8004
    @hamiddoust8004 Před 7 lety

    Awesome explanation !!

  • @stephenchen2011
    @stephenchen2011 Před 7 lety

    It's a nice video, curious, what happen if array starts with 0, such as {0}, or {0, 1, 3}?

    • @ByteByByte
      @ByteByByte  Před 7 lety +1

      pretty much by definition, we need to start with 1, because otherwise you can't differentiate between an array missing 0 or missing a number at the end

  • @coolgoose8555
    @coolgoose8555 Před 5 lety

    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?

  • @khemchandrs
    @khemchandrs Před 4 lety

    Is given array is sorted?

  • @basheeral-momani2032
    @basheeral-momani2032 Před 3 lety

    You are awesome

  • @robinthakur7050
    @robinthakur7050 Před 6 lety

    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

    • @ByteByByte
      @ByteByByte  Před 6 lety

      this solution is already O(n). And sum of squares is a bit dangerous considering overflow

    • @robinthakur7050
      @robinthakur7050 Před 6 lety +1

      Byte By Byte Thanks for the reply I just thought that it was simpler .In fact using this it should tkae constant time ...keep up the gud work buddy..

  • @anuragpundir1483
    @anuragpundir1483 Před 6 lety

    Dude you are awesome , Keep up the good work (thumbsup)

  • @Nmx7428
    @Nmx7428 Před 4 lety

    what if we take input form user
    what is expected code change we have to made

  • @gepliprl8558
    @gepliprl8558 Před 4 lety

    Thank you man. 👌☝👍

  • @milpitas1985
    @milpitas1985 Před 5 lety

    Running time is incorrect unless it is given that input array is sorted.

  • @aamirjamal6833
    @aamirjamal6833 Před 5 lety +1

    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?

  • @alejandroguzman4619
    @alejandroguzman4619 Před 4 lety

    Does it work for arrays that are not sorted?

  • @mohdrasid6737
    @mohdrasid6737 Před 3 lety

    must watch 🥰

  • @alexandruteodor3585
    @alexandruteodor3585 Před 4 lety

    Wouldn't it be much simpler to solve this problem with an occurence array?

  • @mrizigarouiass6474
    @mrizigarouiass6474 Před 5 lety

    !!! 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)
    Thanks

  • @sparrownature9189
    @sparrownature9189 Před 5 lety

    How about if first 2 numbers missing, for example [3,4,5]

  • @alexandersmirnov4274
    @alexandersmirnov4274 Před 10 měsíci

    size * (size + 1) / 2 not (size - 1)

  • @avinashpathak7563
    @avinashpathak7563 Před 6 lety

    what about when we want find k missing number in range 1 to n

  • @scottstoll-dn2yx
    @scottstoll-dn2yx Před 5 lety

    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!)

  • @b11918
    @b11918 Před 7 lety

    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 !!!

    • @ByteByByte
      @ByteByByte  Před 7 lety +1

      yes you could use built in functions to do the work for you but that kind of defeats the purpose

    • @b11918
      @b11918 Před 7 lety

      defeats the purpose ! why ? i am a fresher and if in interview if this same question is asked and i answer it through set_difference will it be wrong ?

    • @ByteByByte
      @ByteByByte  Před 7 lety +2

      its like me asking you to sort an array and you using Arrays.sort. Yes it gives the same result but the only thing I learn about you is that you know that Arrays.sort exists.