Merge Sort - Swift Tutorial - iOS Interview Coding Challenge

  Переглядів 23,232

Sean Allen

Sean Allen

6 років тому

The next video in my iOS Interview questions series all about coding challenges explains a common sorting algorithm called Merge Sort in Swift. I have been asked to code this during a phone screen interview for an iOS position, so it's good to know for a variety of reasons.
iOS Dev Courses - seanallen.teachable.com
Thomas Hanning Blog Post about Merge Sort:
www.thomashanning.com/merge-so...
Harvard CS50 Merge Sort:
• Merge Sort
Xcode Project Source Code:
www.dropbox.com/sh/yi8z92z8km...
iOS Dev Courses:
seanallen.teachable.com/
Twitter:
Sean Allen - / seanallen_dev
Hired.com:
hired.com/x/1n01g
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Paul Hudson's Hacking With Swift:
gumroad.com/a/762098803
Donny Wals - Combine:
gumroad.com/a/909014131
Mark Moeyken’s SwiftUI Books:
www.bigmountainstudio.com/swiftui-views-book/fzc51
Objc.io Books (Thinking in SwiftUI & Advanced Swift):
gumroad.com/a/656585843
Ray Wenderlich Books:
store.raywenderlich.com/a/208...
#swift #softwaredeveloper #iosdeveloper

КОМЕНТАРІ: 67
@seanallen
@seanallen 4 роки тому
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ukposts.info/have/v-deo/hYN6mWiwbXyWxmw.html
@smritibharti1930
@smritibharti1930 3 роки тому
removeFirst is increasing time complexity, better to use temp variable of indexes
@crossfitbeast805
@crossfitbeast805 6 років тому
Love these videos. Been teaching myself swift and I cannot stress enough how useful these are.
@seanallen
@seanallen 6 років тому
Glad the videos are helping, Don!
@alphonsosensleyii7630
@alphonsosensleyii7630 5 років тому
Nice!! Sean, you are the man! I am a little over a year into my iOS Dev journey and your videos really encourage me to keep pushing. I have yet to come across any other online tutorials that give such in depth content while still being easy to understand. Pretty sure if you created a book it would sell like crazy. Anyway, Hope you are enjoying the new gig! and thanks for sharing your knowledge!
@seanallen
@seanallen 5 років тому
Thanks Alphonso! I appreciate the kind words and glad you're enjoying the videos. The new gig is going great so far 👍
@mohsinkhan845
@mohsinkhan845 3 роки тому
best mergesort implementation i have ever seen. thannks man
@YoelEphotography
@YoelEphotography 6 років тому
Hi Allen, Love those algorithm tutorials, you are straight to the point adding a nice recap at the end. BTW you can print("Your text"," ") instead of just having another print func
@seanallen
@seanallen 6 років тому
Glad you enjoyed the video, Yoel! And thanks for the tip 😀
@smael1989
@smael1989 4 роки тому
Great video, great explanation! Thank you!
@seanallen
@seanallen 4 роки тому
Happy to help!
@colonelkob
@colonelkob 6 років тому
good video Sean, ill have a go at a different approach at the weekend when i'm feeling better
@seanallen
@seanallen 6 років тому
Glad you enjoyed it, Ben!
@vaishnaviyerpude29
@vaishnaviyerpude29 Рік тому
Well explained thank you
@laurapotter6321
@laurapotter6321 5 років тому
i love this!!
@demolition3612
@demolition3612 Рік тому
Wish I watched this before my interview, never heard of a merge sort before then and ran out of time before I finished my function
@seanallen
@seanallen Рік тому
Now you're ready for it next time :)
@jenna6132
@jenna6132 6 років тому
Fun question, normally I expect merge sort to retain order. For example if you're sorting objects by some calculated hash value, you might want the leftmost object to stay on the left when hashes are equal. To do that with your example, shouldn't the line comparing left.first and right.first be "less than or equal to", rather than "less than"? That way, merge would first append from the left and then append from the right on the next iteration.
@pratikgupta7373
@pratikgupta7373 3 роки тому
393 likes and 0 dislikes this is the power of sean by the way great video
@protaotogamer1503
@protaotogamer1503 4 роки тому
Excellent tutorial 😃😃
@seanallen
@seanallen 4 роки тому
Glad you liked it, Ahaan!
@protaotogamer1503
@protaotogamer1503 4 роки тому
Nice vid and very nice explanation but I didn't understand why is the return part of mergeSort recursive / repeating
@isaacclark9825
@isaacclark9825 6 років тому
Very clear explanation of mergesort. Nice! Have you been asked to do insertion sort, heap sort, or selection sort in an interview?
@seanallen
@seanallen 6 років тому
Hey Isaac, I have not been asked about those others, but I have seen Quick Sort and Bucket Sort show up on a list of "Things you may be asked about" by a company recruiter in preparation for final interviews. However, they weren't asked in that specific interview.
@isaacclark9825
@isaacclark9825 6 років тому
Thanks Mr. Allen. God luck with your job search.
@Rousen90
@Rousen90 6 років тому
Thank you for these swift videos, it is helping me learn swift immensely. I was curious if there is any issue using
@seanallen
@seanallen 6 років тому
It doesn't matter either way. The case where it is equal will only come up when there is the same number in both the left and right array. And in this case it doesn't matter which duplicate you add to the mergedArray first. Either way will work. (Using just < or
@jo1717a
@jo1717a 6 років тому
I know this is getting too specific, but I would assume implementing merge sort, we care about run time. Utilizing removeFirst itself has a runtime of O(n). Your merge sort implementation with the removeFirst method will cause your runtime to be exponential.
@guruteja7151
@guruteja7151 6 років тому
Nice tutorial Sean. What is the algorithm used in swift default sort function?
@seanallen
@seanallen 6 років тому
Not sure off the top of my head, but here's a link to a StackOverflow post discussing it. stackoverflow.com/questions/41031106/which-general-purpose-sorting-algorithm-does-swift-use-it-does-not-perform-well/41070802
@anandnimje5750
@anandnimje5750 6 років тому
How to deal with High quality images inside only one image for all sizes of devices iPhone and iPad what kind of images we need to use?
@donathmm3881
@donathmm3881 4 роки тому
My brain is like 🤯
@starecho0105
@starecho0105 6 років тому
Thanks for making this tutorial and also thanks for recommending awesome "THOMAS HANNING" website.
@seanallen
@seanallen 6 років тому
Happy to help!
@vigneshrajsb
@vigneshrajsb 5 років тому
I tried to print the merged array and the left & right arrays every time the function is called. looks like the loop runs through twice. Can you please tell me why this is happening?
@preetamjadakar3764
@preetamjadakar3764 4 роки тому
one more optimization in addition to removeFirst() method as it involves shifting, what if elements are the same? this algo is still changing the order instead of keeping as is! I think this the beauty of Merge Sort! In other words: Merge sort is stable but you implemented it as unstable. Need to add equal to case in while loop. Please!
@olegk5408
@olegk5408 6 років тому
Thanks for video Sean! Really good and useful one, as usual. But there is already exist build in sort array method called array.sorted(by:) Is it working slower or depend on cases some methods working better and need to write them by yourself? And if yes, in which cases each method working better?
@seanallen
@seanallen 6 років тому
+Олег Куплин You're right, there is a sorted() function in Swift. These coding challenges are the types of questions you'll see in interviews that'd you'll either have to code on a whiteboard or during a phone screen. It's not really meant to replace he actual sort() method. These are just coding exercises you'll see in interviews.
@olegk5408
@olegk5408 6 років тому
Sean Allen got it Sean. Thanks for what you’re doing. Wish you more subscribers in future and career growth! Enjoying every your video
@seanallen
@seanallen 6 років тому
+Олег Куплин thanks for the kind words! The channel is only 3.5 months old, and I'm having fun with it. More videos to come!
@mdrashidiqubal1188
@mdrashidiqubal1188 5 років тому
nice...
@seanallen
@seanallen 6 років тому
Need help understanding Merge Sort in Swift? Leave a comment, I'm happy to help!
@matthewguest8606
@matthewguest8606 6 років тому
Nice video Sean, really helpful! Could you do one on Quicksort? That algorithm has always confused me.
@seanallen
@seanallen 6 років тому
+Matthew Guest Hey Matthew, glad you enjoyed the video. Quicksort is on the list, so I'll get to it eventually. So many videos to make, so little time, lol.
@user-yd9xy3rb4x
@user-yd9xy3rb4x 3 роки тому
I got it now. I should always watch the whole video instead of just looking at the code.
@seanallen
@seanallen 3 роки тому
No worries 😀
@vamsikamjula7335
@vamsikamjula7335 6 років тому
Hello Sean, great videos !!!!! We can also do sorting in simple way by using following lines of code... var numbers = [9, 8, 7, 6, 3, 2, 1, 4, 5] numbers = numbers.sorted(by: { $0 < $1 }) print(numbers) Is this recommended or we have to do a recursion in whiteboard challenge...????
@seanallen
@seanallen 6 років тому
For sure, there are shorter ways to do my "Coding Challenge" videos, however when you're doing a whiteboard interview, they typically want you to do it the long way. Anyone can just type .sorted to sort something, but the point of the interview is to see how you think, and how you would solve to problem. It's not so much about getting a result. It's how HOW you got it.
@vamsikamjula7335
@vamsikamjula7335 6 років тому
Thanks !!!!
@orampo
@orampo 6 років тому
What algorithm does in-built iOS sort or sortBy function uses? what is the time and space complexity?
@seanallen
@seanallen 6 років тому
I had to look this up. You can find more info and links here: stackoverflow.com/questions/41031106/which-general-purpose-sorting-algorithm-does-swift-use-it-does-not-perform-well/41070802
@orampo
@orampo 6 років тому
Thank you so much. I would like to interact with you about iOS. I figured out a way of having mock interview using www.pramp.com/ . I guess this will be mutually beneficial and solve our interaction purpose as well. I am an iOS dev from India. www.pramp.com/invt/yBrKN5aMQ5sozbG6BNjV
@ombhagwan859
@ombhagwan859 6 років тому
After sorting I got this result result : [5, 4, 2, 6, 7, 3, 1, 8] while I have followed your steps.
@seanallen
@seanallen 6 років тому
Did you try to download the source code from the description, and compare your code with mine?
@iOS_Developer_Swift
@iOS_Developer_Swift 2 роки тому
the problem is that our code is not working Sean.
@MortalKombatant
@MortalKombatant 5 років тому
Sir firebase tutorial I'm waiting for it
@seanallen
@seanallen 5 років тому
I never use Firebase, so that probably won't happen for a while.
@jaiswalji1
@jaiswalji1 6 років тому
you are near 2k
@seanallen
@seanallen 6 років тому
+Yogesh Jaiswal It's rapidly approaching...
@user-yd9xy3rb4x
@user-yd9xy3rb4x 3 роки тому
About elements and indexes, 🤦‍♂️ my bad. I didn’t notice “
@user-yd9xy3rb4x
@user-yd9xy3rb4x 3 роки тому
Doesn’t work anyway. I will send a like to my github. I do want to figure it out.
@berkat07
@berkat07 2 роки тому
Attempt this and does not work for me -> even copied and paste you code into a playground
@berkat07
@berkat07 2 роки тому
sorry a mistake on my end with naming convention
@lulaputta
@lulaputta 6 років тому
Great videos, but you're little bit too fast. And btw, you look like Tinkernut (ukposts.info).
@seanallen
@seanallen 6 років тому
Thanks Badhan, I'm working on slowing down a bit.
Two-Sum Problem - Swift Tutorial - iOS Interview Coding Challenge
17:33
Learn Merge Sort in 13 minutes 🔪
13:45
Bro Code
Переглядів 231 тис.
BRAWLER MUTATIONS WILL BREAK THE GAME! - Brawl Talk
09:34
Brawl Stars
Переглядів 25 млн
Кровосток - разговор с легендами / вДудь
2:12:57
Это было легко! Оживили и Отдаем BMW владельцу!
1:40:26
ИЛЬДАР АВТО-ПОДБОР
Переглядів 3,3 млн
Algorithms: Merge Sort
9:53
HackerRank
Переглядів 616 тис.
Coding Interviews Be Like
5:31
Nicholas T.
Переглядів 6 млн
Swift: Merge Sort Algorithm (iOS Interview Prep) 2022
10:34
iOS Academy
Переглядів 6 тис.
Sort List - Merge Sort - Leetcode 148
13:17
NeetCode
Переглядів 62 тис.
iOS Concurrency and Threading - iOS Interview Question - Swift
7:50
Sean Allen
Переглядів 104 тис.
How to store ordered data in arrays - Swift for Complete Beginners
14:19
How I Got Good at Coding Interviews
6:29
NeetCode
Переглядів 1,6 млн
MVVM + Dependency Injection in Swift | Unit Testing | iOS
29:29
Learn Quick Sort in 13 minutes ⚡
13:49
Bro Code
Переглядів 254 тис.
Опасная флешка 🤯
0:22
FATA MORGANA
Переглядів 298 тис.
СКОЛЬКО ЕЩЕ БУДЕТ АКТУАЛЕН IPHONE 13?
14:10
DimaViper Live
Переглядів 40 тис.