InterviewStack.io LogoInterviewStack.io

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.

MediumTechnical
66 practiced

Implement a brute-force k-NN search in Python for dense float32 embeddings: given a query matrix Q (m x d) and database D (n x d), return for each query the indices of k nearest neighbors by cosine similarity. Discuss computation complexity, memory trade-offs, and vectorization strategies (NumPy / PyTorch) you would use to accelerate it for moderate n (<= 1M) and d (<= 512).

HardSystem Design
49 practiced

Design an external k-way merge strategy that merges 100k small sorted runs into one final sorted file with minimal passes and bounded memory (e.g., 16GB). Discuss multi-level merge trees, replacement-selection to create longer runs, tournament trees vs min-heap, I/O seek optimization, and how to choose fan-in for each merge level to balance passes vs memory.

MediumTechnical
94 practiced

Explain the semantics and complexity of C++'s std::nth_element and how to achieve equivalent behavior in Python with similar average-case complexity. Discuss use-cases in ML where nth_element-style partial ordering is preferable to full sorting (e.g., top-k inference). Optionally provide a Python sketch using numpy.partition or heapq.

MediumTechnical
62 practiced

Implement least-significant-digit (LSD) radix sort for non-negative integers in Python using base 256 (byte-wise) so that you perform at most 4 passes for 32-bit ints. Ensure each pass is stable and discuss memory/time trade-offs and optimizations for large arrays (n up to 50M).

EasyTechnical
65 practiced

Explain non-comparison sorts: counting sort, radix sort, and bucket sort. For each, describe constraints on keys (range, integer vs. float), stability, time and space complexity, and practical applications in AI/data processing (e.g., histogramming integer features, batching by bucketed keys). When would these outperform comparison sorts for large datasets of integers?

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.