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.
Sorting and binary search practice questions
Find the rotation point (the smallest item) in a rotated sorted array in O(lg n) time. A classic coding interview problem, with a step-by-step solution. keep reading »
Find Repeat, Space Edition »Figure out which number is repeated. But here's the catch: optimize for space. keep reading »
Top Scores »Efficiently sort numbers in an array, where each number is below a certain maximum. keep reading »
Merging Meeting Times »Merge overlapping meeting time ranges into condensed blocks: the classic merge intervals problem, with a step-by-step O(n lg n) solution. keep reading »
Find in Ordered Set »Given an array of numbers in sorted order, how quickly could we check if a given number is present in the array? keep reading »
Keep going with the Sorting, searching, and logarithms section of our coding interview course.
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?
What is the time complexity of binary search?
What's the difference between merge sort and quicksort?
Can binary search work on an array that isn't fully sorted?
What does it mean for a sorting algorithm to be stable?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Greedy algorithm interview questions
- Binary tree and BST interview questions
- Graph interview questions
- Dynamic programming and recursion interview questions
- Stack and queue interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake