Searching and sorting algorithms with time complexity
Filed under Amazon
Free read 1 of 3Linear search: O(n), works on unsorted data. Binary search: O(log n), needs sorted data and random access. Hash table lookup: O(1) average, O(n) worst case. BST lookup: O(log n) if balanced, O(n) if skewed.
The question
Explain common searching and sorting algorithms and their time complexities.
Approach
Linear search: O(n), works on unsorted data. Binary search: O(log n), needs sorted data and random access. Hash table lookup: O(1) average, O(n) worst case. BST lookup: O(log n) if balanced, O(n) if skewed.
Bubble, selection and insertion sort are O(n^2) time with O(1) space. Insertion sort is O(n) on nearly sorted input and is stable, which makes it good for small arrays.
Merge sort: O(n log n) always, stable, O(n) extra space. Quicksort: O(n log n) average, O(n^2) worst case, in place, usually fastest in practice. Heap sort: O(n log n) always, in place, not stable.
Counting sort O(n + k) and radix sort O(d(n + k)) beat n log n when keys are small integers. Any comparison sort needs at least O(n log n). Choose by data size, stability needs, memory limits and how sorted the input already is.
Tips
- What they are testing: This question tests whether you know complexities and can say when to choose each algorithm, including stability and space.
- Common mistake: Quoting quicksort as always O(n log n), without mentioning the O(n^2) worst case.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.