54 sorting interview questions [easy, medium, hard]

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:

  1. Easy sorting interview questions
  2. Medium sorting interview questions
  3. Hard sorting interview questions
  4. 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 
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

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 
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
Question 29: Insertion sort list
Question 30: Largest number
Question 31: Kth largest element in an array
Question 32: Custom sort string
Question 33: Queue reconstruction by height
Question 34: Non-overlapping intervals

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
Question 36: Merge k sorted lists
Question 37: Count of smaller numbers after self
Question 38: Count of range sum
Question 39: Reverse pairs
Question 40: Orderly queue
Question 41: Create sorted array through instructions
Question 42: Minimum cost to hire K workers
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. 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:

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:

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.

cta_illustration arrow_left Browse FAANG ex-interviewers

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!