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
Ай бұрын
Frequency count or cyclic sort or hashmap etc
@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.
@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
@sharatchandra1586
2 жыл бұрын
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
2 жыл бұрын
Exactly! so he actually failed.
@nameless2850
Жыл бұрын
Wouldnt using XOR better??
@aakashgupta7211
Жыл бұрын
@@nameless2850 Will it be considered as an extra space?(To store the XOR's in a variable)
@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
Жыл бұрын
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; }
@GSuncin
3 ай бұрын
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
@n_fan329
9 ай бұрын
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
@razgandooverbo
2 жыл бұрын
Best comment ever, start with any solution, and dont try to give the best solution at first. Better to have something then nothing
@mayurdugar03
2 жыл бұрын
Take sum and subtract from sum(1 to n) to get the duplicate value.
@RL-mv9hj
2 жыл бұрын
My first thought lol!
@nateh379
Жыл бұрын
sum(all) - n*(n+1)/2
@S__Arslaan
11 ай бұрын
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
11 ай бұрын
@@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
3 ай бұрын
@@S__Arslaanthe array would be 1,2,2,3
@stinkycoder5043
6 ай бұрын
She said, without modifying the array but the solution provided modifies the array.
@gabriellemarie13
28 күн бұрын
I am fairly new to coding, how can I start preparing for technical interviews and land a job as a software engineer?
@tryexponent
23 күн бұрын
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!
@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
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! 💪
@unmeshchougule5666
2 жыл бұрын
Question asked not to modify the array
@aricanadietv3795
2 жыл бұрын
I was thinking the same.
@HariKrishnan-ff4hf
2 жыл бұрын
We can again able to restore back the original elements . for(int i=0;i
@aup720
2 жыл бұрын
@@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
2 жыл бұрын
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
11 ай бұрын
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
@praneethp-j9l
8 ай бұрын
you were not supposed to modify the array right
@ahmadharis4u
2 жыл бұрын
Iterate once finding the array sum, then subtract the sum of n numbers : Ans = ArrSum - (n*(n+1)/2)
@AkshayMundotia
2 жыл бұрын
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
2 жыл бұрын
The number can be repeated more than twice too. That's why it won't work.
@AkshayMundotia
2 жыл бұрын
@@sharatchandra1586 Hmmmm. Feel like someone should clarify that. Am I the only one would infer 2 times by the word duplicate?
@ahmadharis4u
2 жыл бұрын
@@sharatchandra1586, as per my understanding, duplicate means 2 unless stated otherwise.
@ahmadharis4u
2 жыл бұрын
@@sharatchandra1586 also, there are only n+1 numbers in the array, that stresses more on just one number repeated twice.
@tekbssync5727
2 жыл бұрын
he is modifying the array which we can't do , right?
@sky_beast5129
2 жыл бұрын
Yep.
@MoMoGammerOfficial
2 жыл бұрын
as per the constraint yes. he modified and thus did not meet the requirements.
@sky_beast5129
2 жыл бұрын
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
2 жыл бұрын
what is this algorithm called?
@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
2 жыл бұрын
@@sky_beast5129 Thank you, i did not know about this formula.
@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.
what if there is a greater element than length of array? then it will throw arrayindexoutofbound exception
@chris5000r
8 ай бұрын
yes
@lansi3608
2 жыл бұрын
leetcode 268 just xor all elements and 1 to n
@NickKravitz
2 жыл бұрын
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
2 жыл бұрын
Yeah too bad, you would have been rejected again. Doesn’t work for [1,1,1,2]
@sky_beast5129
2 жыл бұрын
@@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
2 жыл бұрын
@@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
2 ай бұрын
@@sky_beast5129input is perfectly valid
@techpentagon1014
Жыл бұрын
Pretty easy question
@humeidussenejocordasse791
2 ай бұрын
That exercise is mind blowing for me, even with him providing a resolution proposal
@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.
@bharath_v
4 ай бұрын
How would we solve if the input is [1, 100, 0, -1, 100, 50, 60]?
@alay675
6 ай бұрын
I have run this code, it's fails on test cases like [2,2,4,3,1].
@the_hustler01
Жыл бұрын
subtract sum of 1 to n from sum of all elements present in array, simple 🙂
@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)
@arashi.supine
2 жыл бұрын
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
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
2 жыл бұрын
@@MoMoGammerOfficial could you explain what's wrong
@muhammadsharaf1947
2 жыл бұрын
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.
@devangpatel1341
2 жыл бұрын
Can we do sum(1 to n+1) elements - (n*(n+1)/2)=Ans
@sidvyas
2 жыл бұрын
No it will fail for cases which have same number duplicated more than one time
@MoMoGammerOfficial
2 жыл бұрын
@@sidvyas Exactly.
@abhijeetprasad3567
2 жыл бұрын
This solution will work for {1,44,2,44}? I think no
@itsdadannyboy
2 жыл бұрын
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
2 жыл бұрын
@@itsdadannyboy ohh i see i was looking for extended solution and didnt took close look on array constraints 😅😅
@thiagosaife1429
2 жыл бұрын
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
2 жыл бұрын
i also came up with this solution..But that's not constant extra space.. it's O(n) extra space..
@ARATHI2000
Жыл бұрын
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
Жыл бұрын
🤯🤯
@wasifnehvi7705
11 ай бұрын
how does it work for [2,2,2,2,2]
@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
9 ай бұрын
read the question first@@wasifnehvi7705
@jigar22
2 ай бұрын
@@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.
@demyk214
2 жыл бұрын
Bro all these comments just use Copilot
@aaliyahlacole6969
2 жыл бұрын
good job friend!!
@rakeshv1490
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.
@senshi01
2 жыл бұрын
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
2 жыл бұрын
She asked him to keep the space complexity O(1)
@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
@vascanor6085
2 жыл бұрын
XOR(XOR(array), XOR(1 to n))
@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
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.
@rohitraj2321
2 жыл бұрын
Use xor
@abdelrahmanahmed4838
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
2 жыл бұрын
Excellent explanation, thanks
@phanindra5580
2 жыл бұрын
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
Жыл бұрын
First of all he was not supposed to modify the array, if we can then we can do simple cyclic sort, its done
@jigar22
2 ай бұрын
@@darkhunter882we can simply run extra for loop and make all values positive. And we got our input back as it is.
@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
@xudongsun
2 жыл бұрын
Did he modify the array?
@Silverrrrr51
2 жыл бұрын
Yes he did, it was not what was asked at all
@TheHenimoatez
2 жыл бұрын
It works only for positive integers
@itsdadannyboy
2 жыл бұрын
Which is fine, since the problem specifies numbers in range 1 to N
@nithishberadavolu6817
2 жыл бұрын
Sort array and loop array compare ith and i-1 element if both are same, break loop return value
@MrHenryG123
2 жыл бұрын
sorting the array is modifying it
@MoMoGammerOfficial
2 жыл бұрын
@@MrHenryG123 Exactly!
@kevinzebb
Жыл бұрын
@@MrHenryG123 not if it's passed in by value, she never mentioned it had to be passed in by L value reference.
@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
2 жыл бұрын
But this would only work if the array is sorted.. it's not given in the question..
@---xy1mt
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
2 жыл бұрын
Correct me if iam wrong but approx that's O n^2 only.
@---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)..
@MasterOfPlayerRU
2 жыл бұрын
just sum them all?
@akramdiafat9380
2 жыл бұрын
what is the point of summing them?
@alanwang8733
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
2 жыл бұрын
@@alanwang8733 have you tried this approach with some inputs?
@unmeshchougule5666
2 жыл бұрын
(1,2,2)
@alanwang8733
2 жыл бұрын
sum of 1 and 2 is 3 sum of array is 5 duplicate number is 2 no?
@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
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.
@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
2 жыл бұрын
this would work but will not be a constant space solution.
Пікірлер: 144