Sorting & searching interview questions
38 common interview questions across 15 algorithms, each linked to a visual, step-by-step explanation.
Bubble Sort
- What is the best-case time complexity of Bubble Sort?
- How would you detect if an array is already sorted in one pass?
- Why is Bubble Sort considered stable?
- Visualise Bubble Sort
Selection Sort
- Why is Selection Sort not stable by default?
- Compare Selection Sort and Bubble Sort in terms of swaps.
- Can Selection Sort be made stable?
- Visualise Selection Sort
Insertion Sort
- What is the best-case time complexity of Insertion Sort?
- When would you prefer Insertion Sort over Merge Sort?
- Is Insertion Sort adaptive?
- Visualise Insertion Sort
Merge Sort
- Why is Merge Sort preferred for Linked Lists over arrays?
- Can Merge Sort be implemented in-place?
- What is the space complexity of Merge Sort?
- Visualise Merge Sort
Quick Sort
- What is the worst-case time complexity of Quick Sort, and when does it occur?
- How can you mitigate the O(n²) worst-case scenario in Quick Sort?
- Why is Quick Sort generally preferred over Merge Sort for arrays?
- Visualise Quick Sort
Linear Search
- When would you use Linear Search over Binary Search?
- What is the average case comparisons for Linear Search?
- Visualise Linear Search
Binary Search
- Why must the array be sorted for Binary Search?
- Why use mid = low + (high-low)/2 instead of (low+high)/2?
- What is the iterative vs recursive complexity of Binary Search?
- How would you find the first or last occurrence of a duplicate?
- Visualise Binary Search
Cocktail Shaker Sort
- What is the "turtle" problem in Bubble Sort and how does Cocktail Sort address it?
- Is Cocktail Sort asymptotically faster than Bubble Sort?
- Visualise Cocktail Shaker Sort
Comb Sort
- Why does a shrink factor of ~1.3 work well?
- Why is Comb Sort not stable?
- Visualise Comb Sort
Shell Sort
- How does the gap sequence affect Shell Sort’s complexity?
- Why is Shell Sort faster than plain Insertion Sort?
- Visualise Shell Sort
Heap Sort
- Why is building a heap O(n) rather than O(n log n)?
- Why is Heap Sort usually slower than Quick Sort in practice?
- How are the children of index i located in an array-based heap?
- Visualise Heap Sort
Jump Search
- Why is √n the optimal jump size?
- When would you choose Jump Search over Binary Search?
- Visualise Jump Search
Interpolation Search
- What is the worst case and when does it happen?
- Why does interpolation search need uniformly distributed data?
- Visualise Interpolation Search
Exponential Search
- How would you search an infinite sorted stream?
- Why is it O(log i) where i is the target index?
- Visualise Exponential Search
Ternary Search
- Why does ternary search make more comparisons than binary search overall?
- How is ternary search used on unimodal functions?
- Visualise Ternary Search