Microsoft Coding Interview Question - Single Element in Sorted Array (LeetCode)

  Рет қаралды 19,939

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Пікірлер: 46
@MingoDynasty
@MingoDynasty 3 жыл бұрын
I think most people would be afraid to jump +2 or -2 from mid like that, in case of going outside the bounds of the array. Going through two example test cases with array lengths 1 and 3 would be a good idea to show that this is actually always safe.
@vedantshinde2277
@vedantshinde2277 3 жыл бұрын
Yes! You nailed the intuition...we essentially need to see if the subarray is "really" odd.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Yep exactly!
@mananvarma5944
@mananvarma5944 3 жыл бұрын
This was a great intuitive solution, thanks!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad it was helpful!
@rkk1990
@rkk1990 3 жыл бұрын
How do one remember such techniques during the interview ?
@devkunjadia3792
@devkunjadia3792 2 жыл бұрын
I love your content. Keep up the great work!
@bimerinoel4913
@bimerinoel4913 8 ай бұрын
that's very cool. i got something simple also, check this: public static int singleElement(int[] arr) { if (arr.length == 0) return 0; for (int i = 0; i < arr.length; i++){ if (i == arr.length-1) return arr[arr.length-1]; if (arr[i] == arr[i+1]) i++; else return arr[i]; } return 0; }
@surbhiagarwal2322
@surbhiagarwal2322 8 ай бұрын
Good solution. I thought of the same at first, but worst case is O(n) --> element at the last.
@TheRomanianWolf
@TheRomanianWolf 5 ай бұрын
@@surbhiagarwal2322 O(N), both solutions will be rejected.
@haoooyuliu6857
@haoooyuliu6857 4 жыл бұрын
nice explanation, really help for this question!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it helped you, thank you for watching!
@jamesyoo67
@jamesyoo67 4 жыл бұрын
FYI I don't think this works when the last element is the unique number
@vasumahalingam5162
@vasumahalingam5162 4 жыл бұрын
Ifs should check the boundaries; mid-1 >= 0 and nums[mid] == nums[mid-1] and mid + 1 < len(nums) and nums[mid] == nums[mid+1]
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I verified the solution works with the following input: [1,1,2,2,3,3,4,4,8]. The reason why it works is because the if checks check mid - 1 and mid + 1. This problem is pretty tricky because of all the edge cases haha. Thanks for watching!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
@@vasumahalingam5162 Yes, true!
@abhinavsingh-zc2hk
@abhinavsingh-zc2hk 3 жыл бұрын
Thank You so much for this amazing technique.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Most welcome 😊
@sachinmamadapur3000
@sachinmamadapur3000 4 жыл бұрын
Thanks for Nice Explanation :)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem, thank you for watching!
@sivan2878
@sivan2878 Жыл бұрын
Good explanation bro. cool
@ankoor
@ankoor 3 жыл бұрын
Such a great explanation...
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@just_another_curious_guy
@just_another_curious_guy 4 жыл бұрын
Clear explanation, Thank you
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
You are welcome!
@heisenberg1844
@heisenberg1844 4 жыл бұрын
Brilliant. Cheers mate.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem 👍
@swarnimvarshneya6944
@swarnimvarshneya6944 8 ай бұрын
Hey.. I really can't get why after the second condition mid = mid+1 why it's the opposite. pl help anyone.
@surbhiagarwal2322
@surbhiagarwal2322 8 ай бұрын
Ok, line 13. That's because after we determine the subarray is even, in the second condition the duplicate of mid is included in the right side. When we know that the duplicate is in the right side and still the right subarray is odd, that means the one single element is on the right side.
@afk12217
@afk12217 5 ай бұрын
I used a different approach where I calculated the frequency of each element and checked it during each iteration and if it was not 2 then exited the loop and displayed that number....but I could not meet the runtime🤕🤕
@AyaGamal2010
@AyaGamal2010 4 ай бұрын
There is a little better approach than Frequency, according to the sorted array so that you can do a linear search it will be a little better, but binary search is the best of them
@arpit_singh10
@arpit_singh10 4 жыл бұрын
Amazing explanation
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad it was helpful!
@yitingg7942
@yitingg7942 4 жыл бұрын
Michael, can you please please do one for 93. Restore IP Addresses?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Definitely!
@vishnugajulapalli5305
@vishnugajulapalli5305 3 жыл бұрын
What is the white board that you are using?
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
awwapp.com/
@riyazbasha7982
@riyazbasha7982 Жыл бұрын
Can do it for operation
@code7434
@code7434 4 жыл бұрын
Nice Explanation :)
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you!
@bismeetsingh352
@bismeetsingh352 4 жыл бұрын
What do we do when both sides have odd no of elements?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
The logic should be the same if the entire array is odd
@stlo0309
@stlo0309 2 жыл бұрын
i simply went for linear search lol
@antarikshjain6802
@antarikshjain6802 Жыл бұрын
bhai thanks
@psn999100
@psn999100 4 жыл бұрын
AT this TS kzbin.info/www/bejne/anjMaah3r5tpbNU If we know that mid and mid + 1 are same, then we can check for evenness on the left side of mid, Similarly , as you explained , when mid and mid - 1 are the same, we can check for evenness on the right hand side of mid . That would avoid calculation for the evenness only on the right hand side by either doing numOfElements - 1 then % 2 or numOfElements % 2 without a -1 . Does that make sense ? Here is the code that passed Leetcode OJ int singleNonDuplicate(vector& nums) { int n = nums.size(); int low = 0; int high = n - 1; while(low < high){ int mid = low + ((high - low) >> 1); //If mid is itself the number we are looking for, return nums[mid] if(nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) return nums[mid]; if((mid - 1) >= 0 && nums[mid] == nums[mid - 1]){ int numElementsInRHS = high - mid; /*If the num of Elements in the RHS is even, then we know that our single elemnet cannot lie in that half*/ if(!(numElementsInRHS & 1)){ high = mid - 2; }else{ low = mid + 1; } }else if ( (mid + 1 < n && nums[mid] == nums[mid + 1])){ int numElementsInLHS = mid - low; if(!(numElementsInLHS & 1)){ low = mid + 2; }else{ high = mid - 1; } } } return nums[low]; }
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Yes it does, good catch! This problem is challenging because it has several edge cases I think.
BS-8. Single Element in Sorted Array
22:16
take U forward
Рет қаралды 173 М.
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 4,7 МЛН
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 17 МЛН
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 138 МЛН
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
Microsoft Coding Interview Question - Rotate Image
17:21
AlgosWithMichael
Рет қаралды 9 М.
Single Element in a Sorted Array - Leetcode 540 - Python
10:44
NeetCodeIO
Рет қаралды 22 М.
Amazon Coding Interview Question - Number of Distinct Islands
17:43
AlgosWithMichael
Рет қаралды 26 М.
Amazon Coding Interview Question - First Missing Positive (LeetCode)
20:47
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 674 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 193 М.
LeetCode: The Worst Thing to Happen to Software Engineering
8:03
Coding with Dee
Рет қаралды 145 М.
How I Passed The Google Coding Interviews
18:50
Chris Jereza
Рет қаралды 69 М.
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 4,7 МЛН