To ace your coding interview for a software engineering job, you’ll need to understand sorting. It is fundamental to many other algorithms and forms the basis of efficiently solving many problems, from CPU optimization to searching and data retrieval.
Here are some typical questions that involve sorting:
6 typical sorting interview questions
-
You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string. Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.
-
Given an array of integers nums, sort the array in ascending order.
-
Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers. Return any array that satisfies this condition.
-
You are given an array of k linked-lists, and each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it.
-
Given the head of a linked list, return the list after sorting it in ascending order.
- Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it.
Below, we take a look at 54 sorting questions and provide you with links to high quality solutions to them.
This is an overview of what we’ll cover:
-
Easy sorting interview questions
- Medium sorting interview questions
- Hard sorting interview questions
- How to prepare for a coding interview
Let's get started.
1. Easy sorting interview questions
You might be tempted to try to read all of the possible questions and memorize the solutions, but this is not feasible. Interviewers will always try to find new questions, or ones that are not available online. Instead, you should use these questions to practice the fundamental concepts of sorting.
As you consider each question, try to replicate the conditions you’ll encounter in your interview. Begin by writing your own solution without external resources in a fixed amount of time.
If you get stuck, go ahead and look at the solution, but then try the next one alone again. Don’t get stuck in a loop of reading as many solutions as possible! We’ve analysed dozens of questions and selected ones that are commonly asked and have clear and high quality answers.
Here are some of the easiest questions you might get asked in a coding interview. These questions are often asked during the “phone screen” stage, so you should be comfortable answering them without being able to write code or use a whiteboard.
Question 1: Contains duplicate
- Text guide (Medium/punitkmryh)
- Video guide (Nick White)
- Code example (jmnarloch)
Question 2: Valid anagram
- Text guide (Project Debug)
- Video guide (Nick White)
- Code example (OldCodingFarmer)
Question 3: Meeting rooms
- Text guide (Aaronice)
- Video guide (NeetCode)
- Code example (Seanforfun)
Question 4: Assign cookies
- Text guide (Medium/Fatboy Slim)
- Video guide (Kevin Naughton Jr.)
- Code example (fabrizio3)
Question 5: Array partition I
- Text guide (Medium/Len Chen)
- Video guide (Nick White)
- Code example (shawngao)
Question 6: Longest harmonious subsequence
- Text guide (LeetCode)
- Video guide (Algorithms Made Easy)
- Code example (votrubac)
Question 7: Maximum product of three numbers
- Text guide (GeeksForGeeks)
- Video guide (Programmer Mitch)
- Code example (lee215)
Question 8: Sort array by parity
- Text guide (Justamonad)
- Video guide (Kevin Naughton Jr.)
- Code example (lee215)
Question 9: Sort array by parity II
- Text guide (Medium/Fatboy Slim)
- Video guide (Nick White)
- Code example (LeetCode)
Question 10: Special array with X elements greater than or equal X
- Text guide (Medium/Pierre-Marie Poitevin)
- Code example (lenchen1112)
Question 11: Maximum units on a truck
- Text guide (Dev.to/seanpgallivan)
- Video guide (Naresh Gupta)
- Code example (rock)
Question 12: Reorder data in log files
- Text guide (Medium/Annamariya Tharayil)
- Video guide (Michael Muinos)
- Code example (zopzoob)
Question 13: Largest perimeter triangle
- Text guide (GeeksForGeeks)
- Video guide (Helper Func)
- Code example (lee215)
Question 14: Maximize sum of array after K negations
- Text guide (GeeksForGeeks)
- Video guide (Fisher Coder)
- Code example (lee215)
Question 15: Minimum absolute difference
- Text guide (After Academy)
- Video guide (Nick White)
- Code example (gthor10)
Question 16: How many numbers are smaller than the current number
- Text guide (Rishabh Jain)
- Video guide (Xavier Elon)
- Code example (rxdbeard)
2. Medium sorting interview questions
Here are some moderate-level questions that are often asked in a video call or onsite interview. You should be prepared to write code or sketch out the solutions on a whiteboard if asked.
Question 17: Sort an array
- Text guide (cc189)
- Video guide (leetuition)
- Code example (HaelChan)
Question 18: 3Sum
- Text guide (fizzbuzzed)
- Video guide (Nick White)
- Video guide (NeetCode)
- Code example (shpolsky)
Question 19: H-index
- Text guide (TitanWolf)
- Video guide (Algorithms Made Easy)
- Code example (yfcheng)
Question 20: 3Sum closest
- Text guide (Medium/Steven Lu)
- Video guide (Nick White)
- Code example (chase1991)
Question 21: Top K frequent words
- Text guide (LeetCode)
- Video guide (babybear4812)
- Video guide (Michael Muinos)
- Code example (Zirui)
Question 22: Sort characters by frequency
- Text guide (After Academy)
- Video guide (TECH DOSE)
- Code example (rexue70)
Question 23: Top K frequent elements
- Text guide (Webrewrite)
- Video guide (NeetCode)
- Code example (mo10)
Question 24: Contains duplicate III
- Text guide (Yinfang)
- Video guide (Timothy H Chang)
- Code example (lx223)
Question 25: Sort list
- Text guide (Zirui)
- Video guide (NeetCode)
- Code example (jeantimex)
Question 26: Group anagrams
- Text guide (Techie Delight)
- Video guide (TECH DOSE)
- Code example (jianchao-li)
Question 27: Merge intervals
- Text guide (Techie Delight)
- Video guide (NeetCode)
- Code example (brubru777)
Question 28: Sort colors
- Text guide (Medium/Nerd For Tech)
- Video guide (Nick White)
- Video guide (NeetCode)
- Code example (girikuncoro)
Question 29: Insertion sort list
- Text guide (After Academy)
- Video guide (Knowledge Center)
- Code example (mingzixiecuole)
Question 30: Largest number
- Text guide (LeetCode)
- Video guide (TECH DOSE)
- Video guide (jayati tiwari)
- Code example (OldCodingFarmer)
Question 31: Kth largest element in an array
- Text guide (Techie Delight)
- Video guide (Kevin Naughton Jr.)
- Video guide (Back to Back SWE)
- Code example (jmnarloch)
Question 32: Custom sort string
- Text guide (Zirui)
- Video guide (Coding Decoded)
- Code example (rock)
Question 33: Queue reconstruction by height
- Text guide (Medium/codeandnotes)
- Video guide (TECH DOSE)
- Video guide (Knowledge Center)
- Code example (YJL1228)
Question 34: Non-overlapping intervals
- Text guide (Medium/The Startup)
- Video guide (NeetCode)
- Code example (crickey180)
3. Hard sorting interview questions
Similar to the medium section, these more difficult questions may be asked in an onsite or video call interview. You will likely be given more time if you are expected to create a full solution.
Question 35: Maximum gap
- Text guide (Buttercola)
- Video guide (Coding Decoded)
- Code example (zkfairytale)
Question 36: Merge k sorted lists
- Text guide (After Academy)
- Video guide (NeetCode)
- Video guide (Kevin Naughton Jr.)
- Code example (reeclapple)
Question 37: Count of smaller numbers after self
- Text guide (Mithlesh Kumar)
- Video guide (happygirlzt)
- Code example (confiscate)
Question 38: Count of range sum
- Text guide (dume0011)
- Video guide (happygirlzt)
- Code example (dietpepsi)
Question 39: Reverse pairs
- Text guide (Zirui)
- Video guide (take U forward)
- Video guide (happygirlzt)
- Code example (fun4LeetCode)
Question 40: Orderly queue
- Text guide (Massive Algorithms)
- Video guide (Sai Anish Malla)
- Code example (lee215)
Question 41: Create sorted array through instructions
- Text guide (Ruairi)
- Video guide (Lead Coding by FRAZ)
- Code example (lee215)
Question 42: Minimum cost to hire K workers
- Text guide (shengqianliu)
- Video guide (AlgoCademy)
- Video guide (Shiran Afergan)
- Code example (lee215)
Question 43: Russian doll envelopes
- Text guide (Dev.to/seanpgallivan)
- Video guide (Algorithms Made Easy)
- Code example (TianhaoSong)
Question 44: Maximum profit in job scheduling
- Text guide (Tutorials point)
- Video guide (Timothy H Chang)
- Code example (lee215)
Question 45: Max chunks to make sorted II
- Text guide (CodeStorm)
- Code example (shawngao)
Question 46: Find K-th smallest pair distance
- Text guide (Medium/Timothy Huang)
- Video guide (code Explainer)
- Code example (fun4LeetCode)
Question 47: Reducing dishes
- Text guide (tutorialspoint)
- Video guide (code Explainer)
- Code example (jiteshgupta490)
Question 48: Maximum performance of a team
- Text guide (Dev.to/seanpgallivan)
- Video guide (babybear4812)
- Code example (lee215)
Question 49: Check if string is transformable with substring sort operations
- Text guide (shengqianliu)
- Video guide (Elite Code)
- Code example (alanmiller)
Question 50: Allocate mailboxes
- Text guide (Medium/Abhijit Mondal)
- Video guide (code Explainer)
- Code example (goalfix)
Question 51: Maximum height by stacking cuboids
- Text guide (walkccc)
- Video guide (Lead Coding by FRAZ)
- Code example (lee215)
Question 52: Minimum initial energy to finish tasks
- Text guide (Federico Feresini)
- Video guide (Techgeek Sriram)
- Code example (lee215)
Question 53: Checking existence of edge length limited paths
- Text guide (Krammer’s Blog)
- Video guide (Happy Coding)
- Code example (WilMol)
Question 54: Closest room
- Text guide (Krammer’s Blog)
- Video guide (code Explainer)
- Code example (hiepit)
4. How to prepare for a coding interview
4.1 Refresh your knowledge
Before you start practicing interviews, you’ll want to make sure you have a strong understanding of not only sorting but also the rest of the relevant algorithms and data structures. Check out our guides for detailed explanations and useful cheat sheets.
Algorithms explained:
- Depth-first search
- Breadth-first search
- Binary search
- Sorting
- Dynamic programming
- Greedy algorithm
- Backtracking
- Divide and conquer
Data structure guides:
4.2 Practice on your own
Once you’re confident in all of these, you’ll want to start working through lots of coding problems.
For sorting, you can obviously use the questions we’ve listed above. For the other algorithms you need to know, check out the rest of this series:
- Depth-first search interview questions
- Breadth-first search interview questions
- Binary search interview questions
- Dynamic programming interview questions
- Greedy algorithm interview questions
- Backtracking interview questions
- Divide and conquer interview questions
We also recommend working through our list of 73 data structure interview questions.
One of the main challenges of coding interviews is that you have to communicate what you are doing as you are doing it. Talking through your solution out loud is therefore very helpful. This may sound strange to do on your own, but it will significantly improve the way you communicate your answers during an interview.
It's also a good idea to work on a piece of paper or google doc, given that in the real interview you'll have to code on a whiteboard or virtual equivalent.
4.3 Practice with others
Of course, if you can practice with someone else playing the role of the interviewer, that's really helpful. But sooner or later you’re probably going to want some expert interventions and feedback to really improve your coding interview skills.
That’s why we recommend practicing with ex-interviewers from top tech companies. If you know a software engineer who has experience running interviews at a big tech company, then that's fantastic. But for most of us, it's tough to find the right connections to make this happen. And it might also be difficult to practice multiple hours with that person unless you know them really well.
Here's the good news. We've already made the connections for you. We’ve created a coaching service where you can practice 1-on-1 with ex-interviewers from leading tech companies. Learn more and start scheduling sessions today.
Any questions about sorting interview questions?
If you have any questions about sorting or coding interviews in general, don't hesitate to ask them in the comments below. All questions are good questions, so go ahead!