1. Given an unsoorted array of integers nums and an integer target.
Design an algorithm that perform search for the target in the array using linear search.
Return the index of the first occurence of target if found, otherwise return -1.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [2, 4, 6], target = 2 | nums = [2, 4, 6], target = 6 | nums = [], target = 10 |
Output | 0 | 2 | -1 |
Explanation | Found at the first index | Found at the last index | Empty array, target not found |
2. Given a sorted array of integers nums in ascending order and an integer target.
Design an algorithm that perform binary search to find the index of target. If the target exists in the array return its index. Otherwise, return -1.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [2, 5, 8], target = 5 | nums = [1,2,3,...,100000], target = 99999 | nums = [], target = 10 |
Output | 1 | 99998 | -1 |
Explanation | Target found in the first mid-check | Target found after log₂n steps | Empty array — no search possible |
3. Given a Sorted array of integers nums in ascending order and an integer target.
Design an algorithm to find the first and last positions (indices) of target in nums.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 2, 3], target = 2 | nums = [1,1,...,1,2], target = 2 | nums = [], target = 3 |
Output | [1, 2] | [99999, 99999] | [-1, -1] |
Explanation | Target appears consecutively in middle | Target appears only once at the very end (last index) | Empty input list, nothing to find |
4. Given a sorted array of unique integers nums that has been rotated at an unknown pivot index.
For exampe, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2].
Given an integer target. Design an algorithm to search for target in the array.
If target is found, return its index. Otherwise return -1.
A rotated array is formed by taking a sorted array and moving a prefix (one or more elements from the front) to the end, while keeping the relative order of elements unchanged.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [4,5,6,7,0,1,2], target = 6 | nums = [4,5,6,7,0,1,2], target = 3 | nums = [], target = 1 |
Output | 2 | -1 | -1 |
Explanation | Target found at middle index | Target not present, full binary search used | Empty input means nothing to search |
5. Given a sorted array of integers nums and an integer target.
Design an algorithm that returns the total number of times target appears in the array.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 3, 3, 3, 4, 5], target = 3 | nums = [1, 1, 1, ..., 1], target = 1 (100,000 elements) | nums = [1, 2, 3, 4, 5], target = 6 |
Output | 3 | 100000 | 0 |
Explanation | Target 3 appears at indices 2, 3, 4 (3 times) | Entire array is filled with the target 1 | Target 6 is not found in the sorted array |
6. Given an array of integers nums and an integer target. Design an algorithm that determines whether target exists in the array.
If the array is sorted in ascending order, use Binary Search. Otherwise, use linear search.
Return true if target exists in nums, otherwise return false.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 3, 5, 7, 9] target = 3 | [9, 8, 7, 6, 5, 4, 3, 2, 1] target = 1 | [], [5], [5, 5, 5, 5] target = 0, 5, 10 |
Output | true | true | false, true, false |
Explanation | Sorted → Binary search finds in log time | Unsorted → Linear search scans all elements | Empty → return false; One match → true |
7. Given a rotated sorted array of unique integers nums (original sorted in strictly increasing order).
Design an algorithm that find and return the minimum element in the array.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1,2,3,4,5] | [4,5,6,7,0,1,2] | [2], [3,1,2] |
Output | 1 | 0 | 2, 1 |
Explanation | Already sorted, min is first element | Rotated at midpoint, binary search needed | Only one or very small number of elements |
1. Given a rotated sorted array nums of integers, which may contain duplicate values and an integer target. Design an algorithm that determines if target exists in nums.
The array was sorted in ascending order, but then rotated at an unknown pivot index.
For example, [0,1,2,4,4,4,5,6,6,7] might become [4,5,6,6,7,0,1,2,4,4].
Return true if target exists in the array, otherwise return false.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1,2,3,4,5], target = 3 | nums = [2,2,2,3,2,2], target = 3 | nums = [1,1,1,1], target = 2 |
Output | true | true | false |
Explanation | Standard binary search works quickly. | Many duplicates force linear scan. | All values are the same and don’t match target. |
2. Given an array of integers nums. A peak element that is strictly greater than its immediate neighbors.
The array may contain multiple peaks . Design an algorithm that return the index of any one of them.
You may assume that nums[i] != nums[i+1] for all valid i.
You can assume that nums[-1] = nums[n] = -∞ (i.e., elements outside the array are smaller than any element inside the array).
A peak element is an index i such that: nums[i] > nums[i - 1] and nums[i] > nums[i + 1]
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 3, 2] | [1, 2, 3, 4, 5] | [1], [2, 1], [1, 2] |
Output | 1 | 4 | 0 |
Explanation | 3 is a peak with neighbors 1,2 | 5 is the only peak, linear scan reaches end | Single element is trivially a peak. In [2, 1], 2 is a peak. In [1, 2], 2 is a peak. |
3. Given an m * n 2D matrix matrix where:
Given an integer target. Desing an algorithm the determine whether target exists in the matrix.
Return true if target is found, otherwise return false.
Matrix Property:
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [[1,2,3],[4,5,6],[7,8,9]], target=1 | [[1,2,3],[4,5,6],[7,8,9]], target=10 | [[5]], target=5 and [[5]], target=10 |
Output | true | false | true for 5, false for 10 |
Explanation | Found immediately at top-left corner | Target not present, requires full scan | 1x1 matrix checks smallest input edge case |
4. Given a sorted array of lowercase letters called letters and a target letter target.
The array is circular — meaning if the target is equal to or greater than the last character, the search wraps around to the start.
Design an algorithm to find and return the smallest letter in the array that is strictly greater than target.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | ['a', 'c', 'f', 'h'] target = 'a' | ['a', 'c', 'f', 'h'] target = 'h' | ['a', 'b', 'c'] target = 'z' |
Output | 'c' | 'a' | 'a' |
Explanation | 'c' is first char > 'a' | 'h' is last, wraps to 'a' | 'z' > all, so wrap around to 'a' |
5. Given a non-negative integer x. Design an algorithm that computes and returns the integer part of its square root.
In other words, return the floor value of √x, i.e., the largest integer r such that r * r ≤ x.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | 1 | 2^31 - 1 | 0 or 1 |
Output | 1 | 46340 | 0 or 1 |
Explanation | √1 = 1 | √(2³¹−1) ≈ 46340.95 ⇒ floor = 46340 | √0 = 0, √1 = 1 |
6. Given a sorted array of integers arr, an integer k, and a target value x. Design an algorithm that returns the k closest integers to x in the array.
If multiple values are equally close, prefer the smaller one.
Input: arr = [1,2,3,4,5], k = 4, x = 3
Output: [1,2,3,4]
Input: arr = [1,2,3,4,5], k = 4, x = -1
Output: [1,2,3,4]
Input: arr = [1,3,5,7,9], k = 3, x = 6
Output: [3,5,7]
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | 1[1, 2, 3, 4, 5] (balanced around x) k = 1, x = In the middle of arr | [1, 2, 3, ..., 10^5] k = arr.length, x = Far outside array (e.g., x << min or x >> max) | [1, 2, 3, 4, 5] with x not in array k = 0 or k = arr.length x = Exactly equal to multiple elements |
Output | Tight cluster around x | Entire array or farthest edge | Might require tie-breaking by value |
Explanation | Binary search locates x quickly | Full scan required to collect closest k | Tie-breaking logic plays a role |
7. Given two sorted arrays of integers, nums1 and nums2, of sizes m and n respectively.
Design an algorithms that find the median of the combined array formed by merging the two arrays.
Return the median as a floating point number.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | One array is empty | Both arrays large and balanced | One element each, or extreme skew in lengths |
Output | Direct access to middle | Complex partition via binary search | Odd/even total length needs float handling |
Explanation | Target found in 1–2 comparisons | Logarithmic binary partitioning through array boundaries | Special care for array boundaries and null arrays |