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.

EasyTechnical
53 practiced

Explain what 'stable sort' and 'in-place sort' mean with concrete examples. Given the list of tuples [(name, age)]: [('alice', 30), ('bob', 25), ('carol', 30)], show a stable and an unstable sort by age and explain why stability matters in multi-key sorting.

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
46 practiced

Given a sorted array that may contain duplicates, implement in Java int[] searchRange(int[] nums, int target) that returns [firstIndex, lastIndex] if target is present or [-1, -1] otherwise. Use binary search variants to find leftmost and rightmost occurrences in O(log n) time. Show sample inputs (e.g., nums=[5,7,7,8,8,10], target=8 -> [3,4]) and discuss edge cases such as empty array or all-equal array.

EasyTechnical
45 practiced

Given two sorted arrays, return one sorted array containing all elements. Then explain how the solution changes if the inputs are huge streams read one record at a time, and how to keep equal keys in input order.

EasyTechnical
56 practiced

List common off-by-one mistakes and mid-calculation errors that cause binary search implementations to loop infinitely or return incorrect indices. For each mistake give a short example input that breaks the incorrect implementation and explain the root cause.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.