Sorting and Searching Algorithms Questions

Comparison and non-comparison sorting (quicksort with its pivot and partition choices and hybrid cutoffs, mergesort, heapsort as a sort, insertion/selection/bubble, counting/radix/bucket), their stability, in-place behavior, stack depth and lower bounds, and choosing a sort from input constraints, including near-sorted data, library sorts (Timsort, introsort, language defaults), comparator design and expensive comparisons or copies. Also selection (quickselect, median-of-medians, nth_element), external merge sorting of data larger than memory (run generation and merge passes), and binary search with its variants: first/last occurrence, lower/upper bound, rotated arrays with or without duplicates, peak and mountain arrays, sorted matrices, unbounded search, loop-invariant correctness and off-by-one pitfalls, and binary search on an answer space (discrete or continuous, including k-th smallest by value). Includes divide-and-conquer reasoning for sorts (merge step, merging sorted arrays, median of two sorted arrays, inversion counting). Excludes heap data-structure mechanics and heap-based top-k and k-way merging, trees and tries, general graph search, distributed data-processing systems, approximate nearest-neighbor and streaming-sketch algorithms, and abstract Big-O comparison.

MediumTechnical
49 practiced

You must sort a large array of expensive-to-copy objects in a memory-constrained backend process where allocating many temporary objects is costly. Propose a practical, stable approach to sort them with limited additional memory (e.g., using indirection array of indices or pointers, external sort, or stable block-sort). Discuss trade-offs in CPU, memory, and cache behavior.

MediumTechnical
56 practiced

Describe the classic First Bad Version problem where versions are numbered and you have an API isBadVersion(version) that returns a boolean with monotonic behavior. Implement a solution that finds the earliest bad version using O(log n) API calls. Discuss off-by-one pitfalls and test cases.

MediumTechnical
52 practiced

You need to find the minimum substring length L such that there exists a substring meeting a property P (for example, containing at least K distinct characters). Describe how to binary search on length L and use a sliding-window O(n) check to verify property P for a candidate length. Provide complexity analysis and an example.

HardTechnical
57 practiced

Design an external merge sort to sort a 500GB file on disk given only 2GB of RAM. Describe run generation, run size selection, k-way merge strategy, disk I/O optimization, and how to recover from failures mid-sort. Provide rough cost estimates for passes over the data.

HardTechnical
50 practiced

Implement a Python function that searches for a target in a rotated sorted array which may contain duplicates. Signature: def search_rotated_with_duplicates(nums: List[int], target: int) -> bool. Explain why duplicates may force worst-case O(n) behavior and demonstrate how your algorithm handles ambiguous comparisons.

Unlock Full Question Bank

Get access to all Sorting and Searching Algorithms interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.