Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done…

2024

Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done until the element is found?

  1. A.

    1

  2. B.

    3

  3. C.

    4

  4. D.

    2

Attempted by 6 students.

Show answer & explanation

Correct answer: D

Concept: Binary search works only on a sorted array. At each iteration it computes the middle index of the current search range and compares the key to the value stored there: if they match, the search stops; if the key is larger, the left half is discarded and the search continues on the right half; if the key is smaller, the right half is discarded and the search continues on the left half. Each such middle-element comparison counts as one iteration.

The array is already sorted and the question asks for the number of iterations to locate the key — the standard placement-exam phrasing for a binary search trace, so binary search (not a left-to-right linear scan) is the algorithm being counted.

Application: Applying this to arr = [5, 6, 77, 88, 99] (0-indexed) with key = 88:

  1. Iteration 1 — the range is index 0 to 4, so mid = floor((0+4)/2) = 2, and arr[2] = 77. Since 77 is less than 88, the left half (indices 0 to 2) is discarded and the search moves to indices 3 to 4.

  2. Iteration 2 — the range is now index 3 to 4, so mid = floor((3+4)/2) = 3, and arr[3] = 88. This matches the key exactly, so the search stops here.

Cross-check: Binary search on a sorted array behaves like descending an implicit balanced search tree built from the array's midpoints — the first midpoint (index 2) is the tree's root, and the midpoints of each half are its children. Index 3 sits one level below the root in that tree (a child of the {3, 4} half), so reaching it takes exactly one more step after the root — two steps in total, independently confirming the count from the direct trace above.

So the key 88 is found after 2 iterations.

Explore the full course: Coding For Placement

Loading lesson…