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

Given a sorted array (ascending) of integers that may contain duplicates, implement a Python function that returns all unique pairs (value pairs, not indices) whose sum equals target. Signature: def unique_pairs(nums: List[int], target: int) -> List[Tuple[int,int]]. Aim for O(n) time and O(1) extra space (excluding output).

EasyTechnical
92 practiced

Discuss the trade-offs between using the built-in standard library sort in your language of choice (e.g., Python's sorted, Java's Arrays.sort, Node's Array.sort) versus implementing a custom sorting algorithm in a backend service. Cover performance, stability, maintainability, comparator correctness, and edge-case behavior.

MediumTechnical
45 practiced

Implement an algorithm in Java to count inversions in an integer array (number of pairs i < j with arr[i] > arr[j]) using a modified merge sort. Signature: public static long countInversions(int[] arr). Explain how inversion counting fits in O(n log n) time.

MediumTechnical
98 practiced

Design a REST API endpoint for a backend service that returns paginated, sorted user activity records. Requirements: stable ordering with multiple sort keys (timestamp desc, then id asc), support keyset (cursor) pagination and offset pagination, and be resilient to new inserts between pages. Sketch DB schema/indexes and explain why keyset pagination is preferred for large datasets.

HardTechnical
67 practiced

Implement an O(log(min(m,n))) algorithm in Python to find the median of two sorted arrays of lengths m and n. Signature: def find_median_sorted_arrays(a: List[int], b: List[int]) -> float. Explain handling of even/odd combined lengths and provide correctness reasoning for the partitioning approach.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.