Sorting and Searching Algorithms Questions
Comparison and non-comparison sorts (quicksort, mergesort, heapsort, counting/radix), their stability and complexity, and binary search with its many variants. Covers divide-and-conquer reasoning, searching in rotated or implicit spaces, and choosing an algorithm from input constraints. A staple of both fundamentals screens and optimization discussions.
Given a rotated sorted array of unique integers (example: [4,5,6,7,0,1,2]), implement in Python def find_min(nums: List[int]) -> int that returns the minimum element in O(log n) time. Explain the invariant comparing mid with right (or left) and discuss behavior when the array is not rotated (already sorted). Include edge cases such as single-element arrays.
Implement quicksort in C++ using Hoare partitioning and randomized pivot selection. Make sure it sorts in place and include tail-call elimination or an iterative stack to avoid deep recursion. Explain why randomized pivot reduces worst-case on adversarial input.
You are given a comparator supplied by a user that may be inconsistent (non-transitive or throwing exceptions). How would you design a robust sort routine to detect comparator issues, ensure determinism where possible, and fail gracefully? Discuss production safeguards and corrective strategies.
Explain cache-aware and cache-oblivious techniques to improve sorting performance on modern hardware. Describe approaches like blocked partitioning, cache-friendly merges, and sample sort variants. Provide practical recommendations for sorting large arrays in memory-bound situations.
Implement radix sort to sort n non-negative integers using base b. Choose either LSD or MSD approach and justify your choice. Provide Python or C++ code and analyze time complexity in terms of n, b, and number of digits d.
Unlock Full Question Bank
Get access to all Sorting and Searching Algorithms interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.