Sorting and Binary Search Interview Questions

How to approach sorting and searching questions

Sorting shows up in coding interviews in two ways: as questions about the algorithms themselves (how quicksort picks a pivot, which sorts are stable), and as a step inside a bigger solution. Once the input is sorted, duplicates sit next to each other, overlapping ranges can be merged in one pass, and binary search becomes possible.

Binary search finds an item in a sorted array in O(log n) time by halving the range it searches at each step. It also works on things that aren't plain sorted arrays, like a rotated sorted array or a range of possible answers, as long as each step can rule out half of what's left. The tricky part is the boundaries, so trace a two-item input by hand to check that your loop ends.

When you're stuck on an O(n²) solution, ask whether sorting first, in O(n log n) time, would help, and whether the values fall in a small, known range that allows an O(n) counting sort.

Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.

Concepts to review

  • Binary search: Halve the search range at each step to find an item in a sorted array in O(log n) time.
  • Merge sort: A stable O(n log n) sort that splits the input in half and merges the sorted halves.
  • Quicksort: Partitions around a pivot: O(n log n) on average and in place, but O(n²) in the worst case.
  • Counting sort: Sorts integers from a small, known range in O(n + k) time without comparing items.
  • Heapsort: An in-place O(n log n) sort built on a heap.
  • Stable sorting: What it means for a sort to keep equal items in their original order.

Quick reference: Sorting algorithms cheat sheet · Big O cheat sheet

Sorting and binary search interview FAQ

Which sorting algorithm should I use in a coding interview?

Usually your language's built-in sort, which runs in O(n log n) time. Say that's what you're using and mention its complexity. Write a sort by hand only if the interviewer asks you to, or if the input allows something faster, like a counting sort for integers in a small range.

What is the time complexity of binary search?

O(log n), because each step halves the part of the sorted array that could still hold the target. An iterative binary search uses O(1) extra space.

What's the difference between merge sort and quicksort?

Both are divide-and-conquer sorts that take O(n log n) time on average. Merge sort is stable and always O(n log n), but it needs O(n) extra space to merge. Quicksort sorts in place and is often faster in practice, but it takes O(n²) time in the worst case, when the pivots are chosen badly.

Can binary search work on an array that isn't fully sorted?

Sometimes. It needs a way to rule out half of the remaining range at each step. In a sorted array that has been rotated, for example, comparing the middle item with the first item tells you which side of the rotation point you're on, so you can still search it in O(log n) time.

What does it mean for a sorting algorithm to be stable?

A stable sort keeps items that compare as equal in the same order they had in the input. That matters when you sort by one key and then another, like sorting people by name and then stably by age, so people of the same age stay in alphabetical order.
. . .