[DEPRECATED] Software Engineering Mock Interview: Find the Duplicate Number (with Google SWE)

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

ความคิดเห็น • 145

  • @tryexponent
    @tryexponent  9 หลายเดือนก่อน +1

    Don't leave your software engineering career path to chance. Make sure you're interview-ready with Exponent's software developer interview prep course: bit.ly/3wFcUtn

  • @Fam-m4i
    @Fam-m4i 2 หลายเดือนก่อน

    Frequency count or cyclic sort or hashmap etc

  • @GSuncin
    @GSuncin 3 หลายเดือนก่อน +1

    My solution:
    create a Hash Set, for each number in list, add them to the hash, if it's already in it, return it (duplicated)
    O(n) (1 loop)
    set_nums = set()
    for n in range(len(nums)):
    if nums[n] in set_nums:
    return nums[n]
    set_nums.add(nums[n])
    return -1

  • @humeidussenejocordasse791
    @humeidussenejocordasse791 2 หลายเดือนก่อน

    That exercise is mind blowing for me, even with him providing a resolution proposal

  • @sharatchandra1586
    @sharatchandra1586 2 ปีที่แล้ว +46

    It was asked not to modify the array but this answer modifies it. We need to use Floyd's cycle finding algorithm to meet the requirements of using constant space and O(n) time.

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 2 ปีที่แล้ว +7

      Exactly! so he actually failed.

    • @nameless2850
      @nameless2850 2 ปีที่แล้ว +4

      Wouldnt using XOR better??

    • @aakashgupta7211
      @aakashgupta7211 2 ปีที่แล้ว +2

      @@nameless2850 Will it be considered as an extra space?(To store the XOR's in a variable)

    • @mr.mystiks9968
      @mr.mystiks9968 ปีที่แล้ว

      It wouldn’t be hard for him to fix it. Since he max some numbers negative, just loop thru them to make them positive once again. Then return the duplicate.

    • @kjdtm
      @kjdtm ปีที่แล้ว +2

      for those who are l;ooking for the correct solution:
      int findDuplicateCal(int* arr, int size) {
      int n = size - 1;
      // Calculating the expected sum of the first n natural numbers using the formula: n(n+1)/2
      int expected_sum = n * (n + 1) / 2;
      // Calculating the actual sum of the array elements
      int actual_sum = 0;
      for (int i = 0; i < size; i++) {
      actual_sum += arr[i];
      }
      // The duplicate number is the difference between the actual and the expected sum
      return actual_sum - expected_sum;
      }

  • @razgandooverbo
    @razgandooverbo 2 ปีที่แล้ว +17

    Best comment ever, start with any solution, and dont try to give the best solution at first. Better to have something then nothing

  • @unmeshchougule5666
    @unmeshchougule5666 2 ปีที่แล้ว +36

    Question asked not to modify the array

    • @aricanadietv3795
      @aricanadietv3795 2 ปีที่แล้ว +1

      I was thinking the same.

    • @HariKrishnan-ff4hf
      @HariKrishnan-ff4hf 2 ปีที่แล้ว +3

      We can again able to restore back the original elements .
      for(int i=0;i

    • @aup720
      @aup720 2 ปีที่แล้ว +3

      @@HariKrishnan-ff4hf without modifying the array means you can’t change its values in the first place. You should assume it’s a read-only array

    • @user26912
      @user26912 2 ปีที่แล้ว +1

      The previous one I watched (product array except own number) they forgot to mention that the question states no division. That completely changes the question, I thought there's a way faster solution using division and she will probably explain it at the end, not taking the current solution serious (since I had an O(N) solution in mind using the division). Please add the full question then in an overlay so people who watch can properly code along. @Exponent. Thanks for making the content available.

    • @fark69
      @fark69 ปีที่แล้ว

      Sure but in a real interview the interviewer will just point that out and even if he can't figure out the answer (tortoise and hare cycle detection), he'll be praised by the interviewer for being able to come up with this answer

  • @mayurdugar03
    @mayurdugar03 2 ปีที่แล้ว +26

    Take sum and subtract from sum(1 to n) to get the duplicate value.

    • @RL-mv9hj
      @RL-mv9hj 2 ปีที่แล้ว +3

      My first thought lol!

    • @nateh379
      @nateh379 ปีที่แล้ว +1

      sum(all) - n*(n+1)/2

    • @S__Arslaan
      @S__Arslaan ปีที่แล้ว

      No it won't work... If arr=1, 2,2,4 sum Of array=9, sum(1-4) =10... Subtract and you get 1 but the duplicate value is 2

    • @adarshunni4493
      @adarshunni4493 ปีที่แล้ว +2

      ​@@S__ArslaanIt works. And, In the problem statement it's given that the array contains n+1 elements ranging from 1 to n. Therefore the example array you provided doesn't match the problem statement.

    • @b3arwithm3
      @b3arwithm3 4 หลายเดือนก่อน +1

      ​@@S__Arslaanthe array would be 1,2,2,3

  • @ARATHI2000
    @ARATHI2000 ปีที่แล้ว +6

    Duplicate Value = Sum(array) - (N * (N+1))/2. For example, Array = [5, 2, 4, 3, 1, 4], and N=5. Sum(Array) = 19, (5*(5+1))/2 = 15. Difference is the duplicate value = 19 - 15 = 4. Just iterate over array if Index is needed for duplicate value.

    • @tryexponent
      @tryexponent  ปีที่แล้ว

      🤯🤯

    • @wasifnehvi7705
      @wasifnehvi7705 ปีที่แล้ว

      how does it work for [2,2,2,2,2]

    • @chowcao1753
      @chowcao1753 10 หลายเดือนก่อน

      @@wasifnehvi7705 the diff should be within 1 and n, for your case is 4. But it turns out that the diff is 0, therefore, the input is invalid!

    • @ZihadJoy
      @ZihadJoy 9 หลายเดือนก่อน

      read the question first@@wasifnehvi7705

    • @jigar22
      @jigar22 3 หลายเดือนก่อน +1

      ​@@chowcao1753if you check all constraints again then you will realise that [3,3,3,3,3] is also a valid input case.
      Here also all elements are ranging from 1 to 4 if size is 5.
      And she is not saying element is repeated only once.

  • @n_fan329
    @n_fan329 10 หลายเดือนก่อน

    let tab=[1,2,3,5,4,1,6,7,8]
    let repeated;
    tab.forEach(val=>tab.filter(vall=>vall===val).length > 1 ? repeated=val :null )
    console.log(repeated) // 1 & foreach loop doesn't change the original array

  • @ahmadharis4u
    @ahmadharis4u 2 ปีที่แล้ว +27

    Iterate once finding the array sum, then subtract the sum of n numbers : Ans = ArrSum - (n*(n+1)/2)

    • @AkshayMundotia
      @AkshayMundotia 2 ปีที่แล้ว +3

      I don't get why all these mock Interviews are skipping over the simplest/obvious solutions. It's like the candidates don't even take a minute to think about the problem and just jump straight into solutions.

    • @sharatchandra1586
      @sharatchandra1586 2 ปีที่แล้ว +1

      The number can be repeated more than twice too. That's why it won't work.

    • @AkshayMundotia
      @AkshayMundotia 2 ปีที่แล้ว +3

      @@sharatchandra1586 Hmmmm. Feel like someone should clarify that. Am I the only one would infer 2 times by the word duplicate?

    • @ahmadharis4u
      @ahmadharis4u 2 ปีที่แล้ว +2

      @@sharatchandra1586, as per my understanding, duplicate means 2 unless stated otherwise.

    • @ahmadharis4u
      @ahmadharis4u 2 ปีที่แล้ว +1

      @@sharatchandra1586 also, there are only n+1 numbers in the array, that stresses more on just one number repeated twice.

  • @sky_beast5129
    @sky_beast5129 2 ปีที่แล้ว +4

    size_t FindDuplicateNumber(size_t* arr, size_t len)
    {
    size_t sum = 0;
    for(size_t i = 0; i < len; i++)
    sum += arr[i];
    return sum - len * (len - 1) / 2;
    }

    • @iampavel865
      @iampavel865 2 ปีที่แล้ว +1

      what is this algorithm called?

    • @sky_beast5129
      @sky_beast5129 2 ปีที่แล้ว

      @@iampavel865 I don't exactly know what you mean, but the key to this problem is, that the array consists of the first n natural numbers plus one duplicate. For the sum of the first n natural numbers you can use the Gaussian Sum Formula. Henceforth, since the array consists of exactly this sum and one duplicate number, we can retrieve the duplicate by subtracting the sum of the Gaussian Sum Formula.

    • @iampavel865
      @iampavel865 2 ปีที่แล้ว +1

      @@sky_beast5129 Thank you, i did not know about this formula.

    • @sky_beast5129
      @sky_beast5129 2 ปีที่แล้ว

      No problem. If you're wondering why my function uses len * (len - 1) / 2 for the Gaussian Sum Formula, that is because the Gaussian Sum Formula is for the first n natural numbers and the array in this problem has a size of n+1 numbers. So my variable "len" is actually equal to n+1, that's why I needed to replace each n in the formula with (n-1).
      So the Formula is n * (n+1) / 2
      and then becomes
      (n-1) * ((n-1)+1) / 2 = (n-1) * n / 2
      Or just len * (len-1) / 2 with my variable.

  • @eliyoung9406
    @eliyoung9406 4 หลายเดือนก่อน

    Why not just use a hashmap and store the count of each item in the array and then return the number with count 2 in the hashmap.

  • @praneethp-j9l
    @praneethp-j9l 8 หลายเดือนก่อน +2

    you were not supposed to modify the array right

  • @gabriellemarie13
    @gabriellemarie13 หลายเดือนก่อน

    I am fairly new to coding, how can I start preparing for technical interviews and land a job as a software engineer?

    • @tryexponent
      @tryexponent  หลายเดือนก่อน +1

      Hey gabriellemarie13! To ace your technical interviews (as an entry-level SWE), you need to go beyond coding and syntax. You should read up about data structures and algorithms (how to apply them and how to implement them). You should learn about space and time complexity and how to optimize your functions to improve its performance.
      If you want a one-stop platform to learn and prep for your SWE role, you can check out our SWE interview course here: www.tryexponent.com/courses/software-engineering
      Hope this helps!

    • @gabriellemarie13
      @gabriellemarie13 7 วันที่ผ่านมา

      @@tryexponent thank you so much! I will do my research on that :)

  • @stinkycoder5043
    @stinkycoder5043 7 หลายเดือนก่อน +1

    She said, without modifying the array but the solution provided modifies the array.

  • @roshanpadal6227
    @roshanpadal6227 11 หลายเดือนก่อน +1

    what if there is a greater element than length of array? then it will throw arrayindexoutofbound exception

    • @chris5000r
      @chris5000r 9 หลายเดือนก่อน

      yes

  • @techpentagon1014
    @techpentagon1014 ปีที่แล้ว +1

    Pretty easy question

  • @tekbssync5727
    @tekbssync5727 2 ปีที่แล้ว +8

    he is modifying the array which we can't do , right?

    • @sky_beast5129
      @sky_beast5129 2 ปีที่แล้ว

      Yep.

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 2 ปีที่แล้ว

      as per the constraint yes. he modified and thus did not meet the requirements.

  • @mudithadul
    @mudithadul 6 หลายเดือนก่อน

    For me the solution is
    Sum(array) - ( sum((index +1) for n indexes) - array.length)
    Ultimately the idea is the sum of an array element (which has n+1 elements ) - ( sum of 1 to n elements)

  • @bharath_v
    @bharath_v 4 หลายเดือนก่อน

    How would we solve if the input is [1, 100, 0, -1, 100, 50, 60]?

  • @abdelrahmanahmed4838
    @abdelrahmanahmed4838 2 ปีที่แล้ว +2

    Summing all the elements won't work in the case you have an element duplicated more than once ,, the same for xor(xor(arr),xor(1-n)) would work only for even nuber of duplications , so i think he gave the best general solution, great job

    • @muhammadsharaf1947
      @muhammadsharaf1947 2 ปีที่แล้ว +1

      Excellent explanation, thanks

    • @phanindra5580
      @phanindra5580 2 ปีที่แล้ว +4

      question says only one extra duplicate and not more than once . n+1 terms and numbers ranging from 1 to n with one duplicate . 0:05

    • @darkhunter882
      @darkhunter882 ปีที่แล้ว

      First of all he was not supposed to modify the array, if we can then we can do simple cyclic sort, its done

    • @jigar22
      @jigar22 3 หลายเดือนก่อน

      ​@@darkhunter882we can simply run extra for loop and make all values positive. And we got our input back as it is.

  • @arashi.supine
    @arashi.supine 2 ปีที่แล้ว +1

    def find_rep(lst):
    for i in range(len(lst)):
    rep = 0
    if i>0:
    rep= lst[i-1]
    if lst[i] == rep:
    return rep
    return "Nothing's repeating"
    lst = list(map(int,input().split()))
    print(find_rep(lst))
    # This will do?

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 2 ปีที่แล้ว

      You are using extra space so it failed. The correct solution is:
      public static int findDup(int arr[]) {
      for(int i = 0; i < arr.length-1; i++)
      {
      if(arr[i] == arr[i+1])
      return arr[i];
      }
      return -1;
      }

    • @arashi.supine
      @arashi.supine 2 ปีที่แล้ว

      @@MoMoGammerOfficial could you explain what's wrong

  • @ZihadJoy
    @ZihadJoy 9 หลายเดือนก่อน

    int main() {
    int n = 10;
    int arrSum = 0;
    int arr[n+1] = {1,2,3,4,5,5,6,7,8,9,10};
    for(int i = 0; i

  • @muhammadsharaf1947
    @muhammadsharaf1947 2 ปีที่แล้ว +4

    For modifying the array, it could be simply returned back with an extra loop and we would still have it O(2N) = O(N), so I think he has the best solution.

  • @alay675
    @alay675 6 หลายเดือนก่อน

    I have run this code, it's fails on test cases like [2,2,4,3,1].

  • @the_hustler01
    @the_hustler01 ปีที่แล้ว +1

    subtract sum of 1 to n from sum of all elements present in array, simple 🙂

  • @devangpatel1341
    @devangpatel1341 2 ปีที่แล้ว +3

    Can we do sum(1 to n+1) elements - (n*(n+1)/2)=Ans

    • @sidvyas
      @sidvyas 2 ปีที่แล้ว

      No it will fail for cases which have same number duplicated more than one time

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 2 ปีที่แล้ว

      ​@@sidvyas Exactly.

  • @MoMoGammerOfficial
    @MoMoGammerOfficial 2 ปีที่แล้ว

    Since the array was constructed with n+1 order. then following script will suffice:
    int findDup(int arr[]) {
    for(int i = 0; i < arr.length-1; i++)
    {
    if(arr[i] == arr[i+1])
    return arr[i];
    }
    return -1;
    }

    • @AbhishekSharma-vb9ux
      @AbhishekSharma-vb9ux 2 ปีที่แล้ว

      But this would only work if the array is sorted.. it's not given in the question..

  • @chapterme
    @chapterme 2 ปีที่แล้ว +6

    Chapters (Powered by ChapterMe) -
    00:00 - Introduction
    00:40 - Question
    01:19 - Answer
    07:39 - Test cases
    09:30 - Tips

  • @g0nt4
    @g0nt4 ปีที่แล้ว

    Would it using a bitarray with the int go against the rule of using constant space? He is using now set, that's not constant space

  • @senshi01
    @senshi01 2 ปีที่แล้ว +1

    Using a dictionary(hashmap), adding any non existing number to it and returning the number if it exist in it with one for loop, would be O(n) too.

    • @abhilashnataraj5047
      @abhilashnataraj5047 2 ปีที่แล้ว +1

      She asked him to keep the space complexity O(1)

    • @kevinzebb
      @kevinzebb ปีที่แล้ว

      yeah, but need Time Complexity to be O(N). This would be easy and just like two sum in leetcode which would use hash map

  • @NickKravitz
    @NickKravitz 2 ปีที่แล้ว +4

    They should have asked this to a math not CS student. Just sum the array and subtract the triangle formula n*(n+1)/2 to get the answer. Google rejected me twice; too bad I didn't get this problem.

    • @aup720
      @aup720 2 ปีที่แล้ว +6

      Yeah too bad, you would have been rejected again. Doesn’t work for [1,1,1,2]

    • @sky_beast5129
      @sky_beast5129 2 ปีที่แล้ว +3

      @@aup720 It does not work for the input [1,1,1,2] because the input is invalid. The question states, that only one number is repeated in the array.

    • @aup720
      @aup720 2 ปีที่แล้ว +3

      @@sky_beast5129 The input ils perfectly valid, "repeated" doesn't mean it appears only twice. The interviewee in the video took care of checking his solution covers this case.

    • @jigar22
      @jigar22 3 หลายเดือนก่อน

      ​@@sky_beast5129input is perfectly valid

  • @abhijeetprasad3567
    @abhijeetprasad3567 2 ปีที่แล้ว +2

    This solution will work for {1,44,2,44}? I think no

    • @itsdadannyboy
      @itsdadannyboy 2 ปีที่แล้ว +1

      Nums are only in range 1 - n. So if length of arr is 4, you can only have nums from 1-3 and one duplicated number

    • @abhijeetprasad3567
      @abhijeetprasad3567 2 ปีที่แล้ว

      @@itsdadannyboy ohh i see i was looking for extended solution and didnt took close look on array constraints 😅😅

  • @thiagosaife1429
    @thiagosaife1429 2 ปีที่แล้ว +1

    Here's a JS solution, using memoization, O(n) time complexity and not modifying the original array:
    function findDuplicate(arr) {
    const memo = {}
    for (let i = 0; i < arr.length; ++i) {
    if (arr[i] in memo) {
    memo[arr[i]] = memo[arr[i]] + 1
    } else {
    memo[arr[i]] = 1
    }
    }
    return Object.keys(memo).filter((k) => memo[k] !== 1)
    }
    // time complexity: O(n)

    • @AbhishekSharma-vb9ux
      @AbhishekSharma-vb9ux 2 ปีที่แล้ว +2

      i also came up with this solution..But that's not constant extra space.. it's O(n) extra space..

  • @rakeshv1490
    @rakeshv1490 2 ปีที่แล้ว +2

    Marking a[i]-1 value as negative at each index and while doing this, if you already find a negative value, you can simply return i, because that's your duplicate value.

  • @Luckygangff
    @Luckygangff 8 หลายเดือนก่อน

    is this correct
    #include
    int missing_element(const int *a,int &size){
    int total_array=0;
    int sum_of_n=0;
    for(int i=0;i

    • @tryexponent
      @tryexponent  8 หลายเดือนก่อน

      Hey teluguanimetoon2811, thanks for solving it along with us!
      Unfortunately, your solution finds the missing element and not the repeated element so it won't work for this case.
      I think you might be able to tweak the logic in your code to solve this problem though. You've got this! 💪

  • @nithishberadavolu6817
    @nithishberadavolu6817 2 ปีที่แล้ว

    Sort array and loop array compare ith and i-1 element if both are same, break loop return value

    • @MrHenryG123
      @MrHenryG123 2 ปีที่แล้ว +1

      sorting the array is modifying it

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 2 ปีที่แล้ว

      @@MrHenryG123 Exactly!

    • @kevinzebb
      @kevinzebb ปีที่แล้ว

      @@MrHenryG123 not if it's passed in by value, she never mentioned it had to be passed in by L value reference.

  • @vascanor6085
    @vascanor6085 2 ปีที่แล้ว +1

    XOR(XOR(array), XOR(1 to n))

    • @MahmoudAhmed-sc7jg
      @MahmoudAhmed-sc7jg 2 ปีที่แล้ว

      This approach will help you to find the missing number in the array (1 to n) not to get the duplicated one

    • @vascanor6085
      @vascanor6085 2 ปีที่แล้ว

      @@MahmoudAhmed-sc7jg yeah actually I need to do few more steps. After doing xor of all numbers and 1 to n, whatever the result xor will be there, i need to get the right most set bit of that xor result. Then I will xor all numbers which are having that bit set in one variable and the numbers which are not having that bit set in another variable. Now one of the variable contains the duplicate number, we can easily check that with a for loop.

  • @xudongsun
    @xudongsun 2 ปีที่แล้ว +7

    Did he modify the array?

    • @Silverrrrr51
      @Silverrrrr51 2 ปีที่แล้ว +2

      Yes he did, it was not what was asked at all

  • @lansi3608
    @lansi3608 2 ปีที่แล้ว +1

    leetcode 268 just xor all elements and 1 to n

  • @---xy1mt
    @---xy1mt 2 ปีที่แล้ว +2

    Cool trick with the 2nd solution - deriving a hash from index. However, The time complexity of his 1st solution is not O(n2) but better as his inner loop has j=i+1 so the time complexity is the combinations formula i.e (n*(n-1)/2)

    • @varun25aggarwal
      @varun25aggarwal 2 ปีที่แล้ว +1

      Correct me if iam wrong but approx that's O n^2 only.

    • @---xy1mt
      @---xy1mt 2 ปีที่แล้ว

      perhaps i worded it badly (and fixed it now).. i wanted to say that the diff between the quads is big.. and that the combinations one takes far less time.. i.e O(n^2) is approx double that of O(n*n-1/2)..

  • @aaliyahlacole6969
    @aaliyahlacole6969 2 ปีที่แล้ว +1

    good job friend!!

  • @user-ev2pl8ey2r
    @user-ev2pl8ey2r 2 ปีที่แล้ว

    We can count the numbers in a loop (=n), and then check if there is a number greater than one in the new array.
    Am I wrong?

    • @damolaOnikoyi
      @damolaOnikoyi 2 ปีที่แล้ว

      this would work but will not be a constant space solution.

  • @TheHenimoatez
    @TheHenimoatez 2 ปีที่แล้ว +1

    It works only for positive integers

    • @itsdadannyboy
      @itsdadannyboy 2 ปีที่แล้ว +1

      Which is fine, since the problem specifies numbers in range 1 to N

  • @rohitraj2321
    @rohitraj2321 2 ปีที่แล้ว

    Use xor

  • @demyk214
    @demyk214 2 ปีที่แล้ว

    Bro all these comments just use Copilot

  • @MasterOfPlayerRU
    @MasterOfPlayerRU 2 ปีที่แล้ว +1

    just sum them all?

    • @akramdiafat9380
      @akramdiafat9380 2 ปีที่แล้ว

      what is the point of summing them?

    • @alanwang8733
      @alanwang8733 2 ปีที่แล้ว +2

      you can find sum of all numbers 1 to n in constant time then you can subtract that from the sum of the array to find the number that is repeated

    • @unmeshchougule5666
      @unmeshchougule5666 2 ปีที่แล้ว

      @@alanwang8733 have you tried this approach with some inputs?

    • @unmeshchougule5666
      @unmeshchougule5666 2 ปีที่แล้ว

      (1,2,2)

    • @alanwang8733
      @alanwang8733 2 ปีที่แล้ว

      sum of 1 and 2 is 3 sum of array is 5 duplicate number is 2 no?

  • @glenbaker8593
    @glenbaker8593 11 หลายเดือนก่อน

    This is an incorrect answer lol should mention this solution is wrong and why in the video. This is kind of the opposite of providing good interview prep material. I would've rejected this candidate.

  • @hetal.priyadarshi
    @hetal.priyadarshi 2 ปีที่แล้ว

    (1) sum of first n numbers : n*(n+1)/2
    (2) sum of elements in array : sum of first n numbers + duplicate number
    ans = (2) - (1)
    time = O n
    space = O 1

    • @MoMoGammerOfficial
      @MoMoGammerOfficial 2 ปีที่แล้ว

      The requirement says you cant use extra space, and you cant modify the array input as well so summing to find dup is not a solution.