Interview Question: Merge Arrays
Vložit
- čas přidán 13. 09. 2024
- Coding interview question from www.byte-by-byt...
In this video, I show how to merge two sorted arrays.
Do you have a big interview coming up with Google or Facebook? Do you want to ace your coding interviews once and for all? If so, Byte by Byte has everything that you need to go to get your dream job. We've helped thousands of students improve their interviewing and we can help you too.
Stuck on Dynamic Programming? Check out our free ebook: www.dynamicprogrammingbook.com
Need an interview coach? Send in an application: www.byte-by-byte.com/coaching
You can also find me on
Twitter: / bytebybyteblog
Facebook: / bytebybyteblog
Email: sam@byte-by-byte.com
I think the zeros in the first array first need to be moved to the front of the array. Here the contents of array B will always be greater than a since a[aIndex] will always be lesser than b[bIndex].
Thank you for all your amazing videos and great work :)
Thank you for your content! Your videos are a perfect resource to get some additional algorithms in my toolbox. Keep up the good work.
you're welcome!
liked the light blue color of the logo. It distinguishes itself from other videos
Hey Sam,
I think int []a = {1,3,5,0,0,0} array will have a length 6. So we can have aIndex = (a.length - b.length) -1, bIndex = b.length - 1 and mergeIndex = a.length -1. Let me know your thoughts on this. Great video as always!
Yeah you're definitely right. Good catch
Love your videos. Thank you.
You don't need to explicitly pass aLength, and bLength, because bLength = sizeof(b)/sizeof(b[0]). aIndex = aSize - bSize - 1;
hey, nice video. what if we replace the second array in the empty place of the first array(in this case 0,0,0) and then do a simple sort of A ?
Could you just put the B array into the a array based on where the zeros are and then sort it?
That would work as well, but what would be the time complexity of doing that?
Factorial?
What is the time complexity of sorting in general?
@Subhash Mannava exactly! This algorithm runs in linear time and uses no additional space. Unless you count the pointers.
really clear.
thanks!
actually its incorrect, now when you copy the from A[aIndex] to A[mergerIndex] you need to set A[aIndex] as min value, otherwise it wont work.
Could you explain more ?