Searching Questions with Answers

47 practice questions on Searching (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 47 free in the AptiNest app.

Sample questions

Question 1Easy

What is the time complexity of linear search in the worst case?

  1. O(n log n)
  2. O(1)
  3. O(n)
  4. O(log n)

Answer: C. O(n)

Explanation

The key may be last or absent, so all n elements may be checked.

Step-by-step solution

  1. In the worst case the key is last or absent.
  2. Every one of the n elements is checked once.

Why the other options are wrong

  • A. There is no sorting or splitting involved.
  • B. O(1) is only the best case (key at index 0).
  • D. O(log n) needs sorted data and binary search.

Tip: Linear: best O(1), worst O(n).

Question 2Easy

Binary search on the answer finds floor(sqrt(50)) by searching the largest m with m*m <= 50. What value does it return?

  1. 25
  2. 6
  3. 7
  4. 8

Answer: C. 7

Explanation

7*7 = 49 <= 50 but 8*8 = 64 > 50, so the answer is 7. The predicate m*m <= n is monotonic, which is what makes binary search work.

Step-by-step solution

  1. Search m in [0, 50] for the largest m with m*m <= 50.
  2. 7*7 = 49 <= 50 works.
  3. 8*8 = 64 > 50 fails, so the answer is 7.

Why the other options are wrong

  • A. 25 is 50/2, a common starting hi, not the answer.
  • B. 6*6 = 36; 7*7 = 49 still fits.
  • D. 8*8 = 64 exceeds 50.

Tip: Integer sqrt = last m with m*m <= n.

Question 3Easy

Linear search on n elements: if the key is present and equally likely to be at any position, what is the average number of comparisons?

  1. (n + 1)/2
  2. log2 n
  3. n
  4. n/2

Answer: A. (n + 1)/2

Explanation

The average of 1, 2, ..., n is (n + 1)/2.

Step-by-step solution

  1. If the key is at position i, the search makes i comparisons.
  2. Each position is equally likely: average = (1 + 2 + ... + n)/n.
  3. = n(n + 1)/2 / n = (n + 1)/2.

Why the other options are wrong

  • B. log2 n is binary search on sorted data.
  • C. n is the worst case, not the average.
  • D. n/2 is (n + 1)/2 minus 1/2; it undercounts because it ignores that the first position takes 1 comparison, not 0.

Question 4Easy

What does this code print?

import bisect
a = [10, 20, 30, 40, 50]
print(bisect.bisect_left(a, 35), bisect.bisect_right(a, 20), bisect.bisect_left(a, 5))
  1. 3 2 0
  2. 3 1 0
  3. 4 1 0
  4. 3 2 1

Answer: A. 3 2 0

Explanation

35 would be inserted at index 3; bisect_right(20) is just past the existing 20 (index 2); 5 goes at index 0.

Step-by-step solution

  1. bisect_left(a, 35): 35 fits between 30 and 40 -> index 3.
  2. bisect_right(a, 20): just after the existing 20 -> index 2.
  3. bisect_left(a, 5): before 10 -> index 0.

Why the other options are wrong

  • B. bisect_right places 20 after the existing 20, giving 2.
  • C. 35 goes before 40, which is at index 3.
  • D. 5 is smaller than everything, so its index is 0.

Tip: count(x) = bisect_right(a, x) - bisect_left(a, x).

Question 5Medium

Search for 10 in the rotated sorted array [30, 40, 50, 10, 20] with the standard modified binary search (check which half is sorted, then whether the target lies in it). Which indices are probed as mid, in order?

  1. 2, 4, 3
  2. 0, 1, 2, 3
  3. 2, 1
  4. 2, 3

Answer: D. 2, 3

Explanation

mid = 2 (50): the left half [30, 40, 50] is sorted but 10 is not in it, so go right; mid = 3 (10) is found. Probes: 2, 3.

Step-by-step solution

  1. lo = 0, hi = 4, mid = 2 (50). Left half 30, 40, 50 is sorted, but 10 is not in [30, 50].
  2. Go right: lo = 3. mid = (3 + 4) // 2 = 3 (10). Found.
  3. Probes: 2, 3.

Why the other options are wrong

  • A. After mid 2 the range is 3..4, whose mid is 3, not 4.
  • B. Binary search does not scan from index 0.
  • C. 10 is not in the sorted left half, so the search goes right.

Question 6Medium

A sorted array was rotated: [15, 18, 22, 26, 2, 5, 9, 12]. Binary search compares a[mid] with a[hi] to find the minimum. At which index is the minimum?

  1. 6
  2. 3
  3. 5
  4. 4

Answer: D. 4

Explanation

If a[mid] > a[hi] the minimum is right of mid, otherwise at mid or to its left. The search converges on index 4 (value 2).

Step-by-step solution

  1. lo=0, hi=7, mid=3: a[3]=26 > a[7]=12, so lo = 4.
  2. lo=4, hi=7, mid=5: a[5]=5 <= 12, so hi = 5.
  3. lo=4, hi=5, mid=4: a[4]=2 <= 5, so hi = 4.
  4. lo == hi = 4: the minimum 2 is at index 4.

Why the other options are wrong

  • A. Index 6 holds 9.
  • B. Index 3 holds 26, the maximum.
  • C. Index 5 holds 5, just after the minimum.

Tip: The minimum is the only element smaller than its left neighbour.

Question 7Medium

Versions 1 to 100 are built in order; every version from the first bad one onwards is bad. Binary search with mid = (lo + hi) // 2 (hi = mid if mid is bad, else lo = mid + 1) runs until lo == hi. If version 37 is the first bad one, how many isBad calls are made?

  1. 7
  2. 6
  3. 8
  4. 37

Answer: A. 7

Explanation

The range 1..100 halves each call; it takes 7 calls to narrow down to 37.

Step-by-step solution

  1. lo, hi = 1, 100. Calls: mid 50 bad -> hi 50; 25 good -> lo 26; 38 bad -> hi 38.
  2. 32 good -> lo 33; 35 good -> lo 36; 37 bad -> hi 37; 36 good -> lo 37.
  3. lo == hi == 37 after 7 calls.

Why the other options are wrong

  • B. Six halvings are not enough to pin down one value out of 100 (2^6 = 64 < 100).
  • C. Overcounts; the loop stops as soon as lo == hi.
  • D. That is linear search from version 1.

Tip: ceil(log2 100) = 7.

Question 8Medium

Binary search for a peak (an element not smaller than its neighbours) moves right if a[mid] < a[mid+1], otherwise left (keeping mid). Which index does it return for [1, 3, 20, 4, 1, 0]?

  1. 2
  2. 0
  3. 3
  4. 5

Answer: A. 2

Explanation

mid = 2 (20) is not smaller than a[3] = 4, so hi = 2;
then mid = 1 (3 < 20), so lo = 2.
It returns index 2 (value 20).

Step-by-step solution

  1. lo=0, hi=5, mid=2: a[2]=20 > a[3]=4, so hi = 2.
  2. mid=1: a[1]=3 < a[2]=20, so lo = 2.
  3. lo == hi = 2: the peak 20 is returned.

Why the other options are wrong

  • B. Index 0 (1) is smaller than its neighbour 3.
  • C. Index 3 (4) is smaller than 20.
  • D. Index 5 (0) is smaller than 1.

Tip: Walk uphill: move toward the larger neighbour.

Question 9Hard

Stalls are at positions [1, 2, 4, 8, 9]. Place 3 cows in different stalls so that the minimum distance between any two cows is as LARGE as possible. What is that largest minimum distance?

  1. 2
  2. 4
  3. 3
  4. 5

Answer: C. 3

Explanation

Binary search the distance d and greedily place cows left to right whenever the gap is >= d. The largest feasible d is 3.

Step-by-step solution

  1. Sorted stalls: 1, 2, 4, 8, 9.
  2. d = 3: cows at 1, 4, 8. Feasible.
  3. d = 4: cows at 1, then 8 (4 is only 3 away); no third stall >= 12. Not feasible.
  4. Largest minimum distance = 3.

Why the other options are wrong

  • A. 2 is feasible but 3 is larger.
  • B. Gap 4 allows only 2 cows.
  • D. Gap 5 allows only 2 cows.

Tip: Sort the stalls first; greedy placement from the left is the feasibility test.

Question 10Hard

This binary search should find the LAST index i with a[i] <= x. For which input does it loop forever?

def last_le(a, x):
    lo, hi = 0, len(a) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if a[mid] <= x:
            lo = mid
        else:
            hi = mid - 1
    return lo
  1. a = [3, 4, 5], x = 3
  2. a = [1], x = 5
  3. a = [5, 6], x = 1
  4. a = [1, 2], x = 5

Answer: D. a = [1, 2], x = 5

Explanation

With lo = 0 and hi = 1, mid = 0 and a[0] <= x keeps lo = 0 forever.
Using mid = (lo + hi + 1) // 2 (round up) fixes it.

Step-by-step solution

  1. With lo = mid and floor division, if hi = lo + 1 then mid = lo.
  2. For a = [1, 2], x = 5: lo = 0, hi = 1, mid = 0, a[0] <= 5 so lo = 0 again.
  3. Nothing changes, so the loop never ends.
  4. The other inputs shrink the range: [3,4,5] sets hi = 0; [1] never loops; [5,6] sets hi = -1.

Why the other options are wrong

  • A. a[1] = 4 > 3 sets hi = 0, which ends the loop.
  • B. lo == hi from the start, so the loop body never runs.
  • C. a[0] = 5 > 1 sets hi = -1, so the loop ends.

Tip: lo = mid needs an upper mid; hi = mid needs a lower mid.

Practise all 47 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics