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.
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
Question 2: Valid anagram
Question 3: Meeting rooms
Question 4: Assign cookies
Question 5: Array partition I
Question 6: Longest harmonious subsequence
Question 7: Maximum product of three numbers
Question 8: Sort array by parity
Question 9: Sort array by parity II
Question 10: Special array with X elements greater than or equal X
Question 11: Maximum units on a truck
Question 12: Reorder data in log files
Question 13: Largest perimeter triangle
Question 14: Maximize sum of array after K negations
Question 15: Minimum absolute difference
Question 16: How many numbers are smaller than the current number
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
Question 18: 3Sum
Question 19: H-index
Question 20: 3Sum closest
Question 21: Top K frequent words
Question 22: Sort characters by frequency
Question 23: Top K frequent elements
Question 24: Contains duplicate III
Question 25: Sort list
Question 26: Group anagrams
Question 27: Merge intervals
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
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
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
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
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
Question 38: Count of range sum
Question 39: Reverse pairs
- Text guide (Zirui)
- Video guide (take U forward)
- Video guide (happygirlzt)
- Code example (fun4LeetCode)
Question 40: Orderly queue
Question 41: Create sorted array through instructions
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
Question 44: Maximum profit in job scheduling
Question 45: Max chunks to make sorted II
Question 46: Find K-th smallest pair distance
Question 47: Reducing dishes
Question 48: Maximum performance of a team
Question 49: Check if string is transformable with substring sort operations
Question 50: Allocate mailboxes
Question 51: Maximum height by stacking cuboids
Question 52: Minimum initial energy to finish tasks
Question 53: Checking existence of edge length limited paths
Question 54: Closest room
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.
- Depth-first search
- Breadth-first search
- Binary search
- Dynamic programming
- Greedy algorithm
- Backtracking (coming soon)
- Divide and conquer (coming soon)
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 (coming soon)
- Divide and conquer interview questions (coming soon)
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!