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.

HardTechnical
86 practiced

Formalize and prove binary search correctness using loop invariants for both inclusive [low,high] and exclusive [low,high) variants. State the invariant precisely, prove initialization, maintenance and termination conditions, and show how computing mid as low + (high - low)/2 prevents integer overflow in languages with fixed-width integers. Provide a termination argument based on interval shrinking.

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.

MediumTechnical
54 practiced

Given a singly-linked list representing server log entries with timestamps, implement merge sort to sort the list in ascending timestamp order in Python. Signature: def sort_list(head: Optional[ListNode]) -> Optional[ListNode]. You must use O(1) extra linked-list nodes (no arrays) and explain how you find the midpoint.

EasyTechnical
65 practiced

Write a Java method to find the first (leftmost) occurrence of target in a sorted integer array that may contain duplicates. Signature: public static int firstOccurrence(int[] arr, int target). Explain loop invariants and boundary conditions to ensure correctness.

MediumTechnical
45 practiced

Count the number of inversions in an integer array (pairs i < j with a[i] > a[j]) in O(n log n). Explain the recurrence, why the count can need 64 bits, and how equal elements must be handled.

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.