SHORTEST RANGE IN K SORTED LISTS - CODING INTERVIEW QUESTION AT GOOGLE, APPLE OR FACEBOOK

  Рет қаралды 31,853

Success in Tech

Success in Tech

Күн бұрын

Пікірлер: 62
@SuccessinTech
@SuccessinTech 7 жыл бұрын
NEW: Check out my brand new website www.successintech.com
@pagrus7
@pagrus7 7 жыл бұрын
I think space complexity of the naive solution is O(k), since it requires maintaining k pointers/indicies along the way.
@jasonzhang2643
@jasonzhang2643 7 жыл бұрын
I agree with this.
@tianwenchu1663
@tianwenchu1663 6 жыл бұрын
Agree with O(K)
@dgmneto
@dgmneto 5 жыл бұрын
I agree with Pavel
@JamshadAhmad
@JamshadAhmad 4 жыл бұрын
Yup, number of pointers will increase with number of lists.
@aliparsaei
@aliparsaei 4 жыл бұрын
Interesting question. I'd like to note that the worst-case-scenario time complexity of the "naive" & optimized solutions are O(k^2 * n) & O(k * log(k) * n) respectively.
@hanyu4758
@hanyu4758 7 жыл бұрын
Space Complexity should be O(k), for each array you need to keep the index of element of this array when searching the range.
@praveengupta5693
@praveengupta5693 7 жыл бұрын
I think the time complexity for naive approach is O(n*k*k) and for min heap it would be O(n*k*logk) where n is the number of elements in a single array and k is the number of list.
@jasonzhang2643
@jasonzhang2643 7 жыл бұрын
I agree. But I think when he said n, he is referring to the # of numbers of elements in all the lists
@AlissonSiri
@AlissonSiri 6 жыл бұрын
Why do you think that? He would loop through n elements k times, that's O(n*k). Can you explain your thoughts?
@federicomengozzi4785
@federicomengozzi4785 6 жыл бұрын
Alisson Siri loop trough all elements in O(n*k) and find the new minimum with k operation hence O(n*k*k). Whereas with the heap, while iterating over all elements in O(n*k), you could find the minimum in log(k) for a total running time of O(n*k*lg(k))
@Embarassedmess5634
@Embarassedmess5634 4 жыл бұрын
Nice video!!. Its a hard problem in Leetcode. Explanation makes it very simple. Thank you!
@AnonyoX
@AnonyoX 5 жыл бұрын
The space complexity could be O(k) for the k pointers. The only way it could be constant is if (1) the list is a linked-list, not array, and (2) we are using the 'head' node's pointer itself for traversing (and effectively truncating the list(s) as we go). The head node doesn't count as separate space, as it is in the input.
@himanshutanwar1774
@himanshutanwar1774 6 жыл бұрын
Space complexity should be O(k). Because you would be storing the k iterators. Am I right?
@kentnguyen2182
@kentnguyen2182 7 жыл бұрын
Wow did he just said we always increase the min pointer, but he didn't explain why?! that's important
@adityaekawade1060
@adityaekawade1060 7 жыл бұрын
Kent Nguyen The lists are sorted, and we want to find minimize the value of range. So when we move to the next element, we increase the chance to minimize the value of range. For example our list is [0 , 1, 2 , 3] And for first index which is the min value, we get the range value as 5. So when we move to the next index which has the value 1, our range would be 4, and so on..
@SuccessinTech
@SuccessinTech 7 жыл бұрын
What are you hinting at? It's a linear search through the list.
@pagrus7
@pagrus7 7 жыл бұрын
I guess he is hinting at the lack of explanation of why the algorithm is correct; how do we know it will work on other inputs. For me it was helpful to imagine k lists, the desired answer as a set of indicies, and then persuading myself that: - the algorithm will encounter the desired "min" before it encounters the desired "max" (or at the same time, in a special case) - once it encounters the "min" of a minimal range, the pointer for that list won't move until the algorithm sees the "max" of the range. - the "min" pointer also won't move away until every other element is in the [min, max] range ... which means that during the execution the algorithm is ought to see the intended answer.
@SuccessinTech
@SuccessinTech 7 жыл бұрын
+Pavel Grushetzky Very well explained!
@eeyore345
@eeyore345 5 жыл бұрын
@@pagrus7 Thanks for explaining, I was wondering why moving the min forward too.
@anshubansal2008
@anshubansal2008 6 жыл бұрын
Great simple solution for the complex question. Thanks for the video
@cristianouzumaki2455
@cristianouzumaki2455 4 жыл бұрын
Haven't seen a hard level prob explained so simply.
@srikid100
@srikid100 7 жыл бұрын
In addition to Min heap , don't we need a Max heap as well as we need to keep track of the maximum element as well to calculate the range ? Please comment
@SuccessinTech
@SuccessinTech 7 жыл бұрын
+srikid100 No you keep track of the „local“ max (thats how it‘s often called in literature) with k variables, whose value you override whenever the algorithm finds a new max.
@rishidaryanani1333
@rishidaryanani1333 6 жыл бұрын
Success in Tech could you please explain this a little more? From the local min, local max and the min range - all of which require us to look at k elements in the current solution - what exactly goes into the min heap? If we still need to find the local max by going through k values then isn’t the time cost O(k)?
@soumyasengupta7018
@soumyasengupta7018 6 жыл бұрын
Rishi Daryanani we dont go through all the elements to track the local max. 1> first elements from all the lists are initially added to the min heap..while adding we keep track of local min & local max while adding them to heap. 2> when you pop the minimum element from heap and add new elem from same list, there are 2 scenarios. New elem is bigger than rest elems in heap,u compare it with the previously calculated local max and update accordingly. Then you perform heapify again which gives you the min element to be removed again..so up update min variable again and this process goes on until and unless u reach the end of a list. So at a time you process k elements where each element is from a different list. Space complexity is k for heap. Time complexity for intial heapsort of k elements is k_log k. When you remove min from heap and add new element the time complexity of heapify is log k. The above process is repeated at max (nk-k)log k times. So total complexity is (nk-k) log k + k log k = nklogk. Hope this helps you. Happy coding.
@girikgarg1268
@girikgarg1268 3 жыл бұрын
@@soumyasengupta7018 But how do you "keep track of local min & local max while adding them to heap."?
@girikgarg1268
@girikgarg1268 3 жыл бұрын
It would be better if you could explain it through code
@rootchutney6567
@rootchutney6567 7 жыл бұрын
Just curious.. what would be the change be in case of duplicates across lists ? Will a recursive step of increasing multiple min pointers(one by one) be sufficient or is there a better solution out there ?
@7daniel49
@7daniel49 6 жыл бұрын
Bah, I just spent 5 mins typing out why which duplicate min you move matters, but it doesn't. If you just move the min each time, you will cover all necessary ranges. If there are multiple, just move one of the mins, then the next loop will move the next one and so on.
@hrithiktg4054
@hrithiktg4054 5 жыл бұрын
use 3 max-heaps....first..to build a maxheap, it takes O(n) time, n being the length of the list.....later iteratively compare the root of the heaps...find min and max....it takes O(k) time for this....find the range....next, pop the the element from the max-heap whose root was found to be maximum(in other words the heap which has root the greatest).......continue....you'll get it! total time : O(klog(n)) space : O(1)
@manbendrasingh2481
@manbendrasingh2481 5 жыл бұрын
Your algorithm would work but I don't agree with the time complexity you mentioned for your approach. You would end up removing n elements from the heap so you will spend n time also at every step you would also do heapify which would add extra log(n) at every step. So the time complexity for your algorithm would be O(k*n*log(n))
@hrithiktg4054
@hrithiktg4054 5 жыл бұрын
No, "k" lists are already given to you(in this case k=3). You just need to heapify those lists, to get max heap.Heapify takes O(n) time.where "n" is the number of elements in corresponding lists.to find the min, it takes O(k) time.
@hrithiktg4054
@hrithiktg4054 5 жыл бұрын
but yeah, i need to remove the max elements, i takes O(logn) time.so the total complexity is O((k+logn)*N). where, "N" is the sum of the length of the individual list.and "n", lenght of a sigke list.the above complexity is accurate if all the input lists are of length "n".
@rajasubasubramanian9365
@rajasubasubramanian9365 6 жыл бұрын
Just loved it !! Thanks for the video @Ramon Lopez :)
@legendleft
@legendleft 7 жыл бұрын
nice video man! i'll be coming back to your channel
@Dominatoification
@Dominatoification 7 жыл бұрын
good stuff
@edithau8919
@edithau8919 7 жыл бұрын
In your approach, you need to do the min-comparison 4*4*4 times (4-0-5, 4-0-18, 4-0-22, 4-0-30, 10-0-5, 10-0-18....), and hence O(n^k). The time complexity O(n * k) does not seem possible to me. Could you please provide psuedocode for the time complexity O(n * k)?
@federicomengozzi4785
@federicomengozzi4785 6 жыл бұрын
Edith Au actually if you consider n to be the number of elements in each list. The running time for the naive algorithm would be O(n*k*k) because while loop through all elements you would need to find the minim in O(k). In the video he was probably considering n as the number of all elements (n = sum[i in 0..k] #elemList(i))
@nitinagrawal6637
@nitinagrawal6637 5 жыл бұрын
I am not an expert, but saying this approach is Naive may not be correct. Keeping the solutions simples & easy to understand is not a naive approach but can be rather efficient approach in most cases. As coining the terms of other DS like MinHeap, one can boast about his knowledge but it can be dangerous. As if we use MinHeap, then it also takes its own space & time to maintain that structure & here I think, we just need the min at each step & I dont know what significant improvement one can get after using MinHeap here.
@almazglaz
@almazglaz 6 жыл бұрын
isn't it O(n*k*k)? as we also need to move each of k pointers n times and k for searching max/min(which can be optimized to log k)
@dgmneto
@dgmneto 5 жыл бұрын
almazglaz yes, I believe you are correct. At least, that's what I understood from the solution.
@buttofthejoke
@buttofthejoke 5 жыл бұрын
Moving each pointer is a single instruction. When calculating Big O, we ignore such instructions and only calculate number of iterations.
@rumenstoyanov1701
@rumenstoyanov1701 4 жыл бұрын
Yes you are correct, it is one operation for every array entry access which is nk and then a linear search O(k) to find the min/max, hence nk^2
@quantlfc
@quantlfc 2 жыл бұрын
Thanks! :)
@gsniteesh3794
@gsniteesh3794 7 жыл бұрын
Try adding a link to the code for each and every problem you solve
@kratijain
@kratijain 7 жыл бұрын
If 30 in 3rd list is replaced by 23, will the algorithm still be unable to find the minRange??
@prateeksinghal7114
@prateeksinghal7114 3 жыл бұрын
Just a heads up to guys understanding the time complexity. Here n is the total number of elements in all the k lists.
@abhitrades6061
@abhitrades6061 3 жыл бұрын
the code you explained takes O(n*k*k)
@veerrajuyeleti8541
@veerrajuyeleti8541 6 жыл бұрын
sir pls add videous on dynamic programming
@dhiresh5980
@dhiresh5980 5 жыл бұрын
This video felt like going through the algorithm but not explaining it
@swanhtet1
@swanhtet1 Жыл бұрын
Exactly!
@dmitry926
@dmitry926 5 жыл бұрын
O(n * log k) doesn't look correct and it wasn't explained in the video.
@ADITYAKUMAR-yd3ow
@ADITYAKUMAR-yd3ow 6 жыл бұрын
I think time complexity should be O(max(length's of lists) * K). Isn't it?
@jllapin
@jllapin 7 жыл бұрын
Isn't it faster and easier to code to traverse the first array and then move the iterator in every other array to the one that is closest to the current element in the first array? static int[] GetMinRange(List lists) { int[] retVal = null; int[] indexes = new int[lists.Count]; for (int x = 0; x < lists[0].Count; x++) { indexes[0] = x; for (int y = 1; y < lists.Count; y++) { while(indexes[y] < lists[y].Count - 1) { if (lists[0][x] - lists[y][indexes[y]] > lists[0][x] - lists[y][indexes[y] + 1]) { indexes[y]++; } else break; } } retVal = GetMin(lists, indexes, retVal); } return retVal; } static int[] GetMin(List lists, int[] indexes, int [] retVal) { int max = lists[0][indexes[0]]; int min = lists[0][indexes[0]]; for (int x = 0; x < indexes.Length; x++) { if (lists[x][indexes[x]] < min) min = lists[x][indexes[x]]; else if (lists[x][indexes[x]] > max) max = lists[x][indexes[x]]; } if (retVal == null || retVal[1] - retVal[0] > max - min) return new int[] { min, max }; return retVal; }
@PoulJulle-wb9iu
@PoulJulle-wb9iu 4 жыл бұрын
How to disguise the Danish accent lol
@SuccessinTech
@SuccessinTech 4 жыл бұрын
Austrian ;)
@shoooozzzz
@shoooozzzz 6 жыл бұрын
You have to assume there are no duplicates, otherwise this approach will not work. It is impossible to know which x,y,z pointer to increment when there is a tie in the lowest value.
@xiangyuzhao6363
@xiangyuzhao6363 6 жыл бұрын
As far as I see, there won't be a problem if duplicates exist. You can increase either one when there is a tie. Please correct me if I'm wrong, an example would be great.
@7daniel49
@7daniel49 6 жыл бұрын
​@@xiangyuzhao6363 @Ben Wright I was convinced duplicates changed the code, I wrote my solution to address that, but it doesn't. ex. [[14, 17, 21], [14, 34, 35], [20, 21, 24]] - if we prioritize the first list our x, y, z are: 14, 14, 20 -> 17, 14, 20 -> 17, 34, 20 (we have moved from 14s and range is 6) - if we prioritize the second list the x, y, z are: 14, 14, 20 -> 14, 34, 20 -> 17, 34, 20 (moved from 14s, again range is 6) We still look at all the cases, just in a different order.
Perfect Pitch Challenge? Easy! 🎤😎| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 93 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 9 МЛН
Walking on LEGO Be Like... #shorts #mingweirocks
00:41
mingweirocks
Рет қаралды 7 МЛН
Shortest Range in K sorted lists
9:44
IDeserve
Рет қаралды 15 М.
Amazon Coding Interview Question - Recursive Staircase Problem
20:01
How to: Work at Google - Example Coding/Engineering Interview
24:02
Life at Google
Рет қаралды 7 МЛН
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,6 МЛН
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
7 Years of Software Engineering Advice in 18 Minutes
18:32
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 113 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 672 М.
Perfect Pitch Challenge? Easy! 🎤😎| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 93 МЛН