Given the sorted array arr = {2, 5, 7, 99, 899} and key = 899, at what level…

2024

Given the sorted array arr = {2, 5, 7, 99, 899} and key = 899, at what level of recursion is the key found, using recursive binary search and counting the very first call as level 1?

  1. A.

    5

  2. B.

    2

  3. C.

    3

  4. D.

    4

Attempted by 8 students.

Show answer & explanation

Correct answer: C

Recursive binary search repeatedly examines the middle element of the current search range, using the standard lower-midpoint convention mid = floor((low + high) / 2), and stops as soon as the key matches that middle element; otherwise it recurses into the left half (key smaller than the middle) or the right half (key larger than the middle), discarding the other half entirely. The "level of recursion" is simply how many such middle-element checks (recursive calls) are made before the key is located, counting the very first call as level 1.

  1. Level 1: the search range is the full array (indices 0 to 4): 2, 5, 7, 99, 899. The middle index is 2, so mid = 7. Since 899 is greater than 7, the search recurses into the right half (indices 3 to 4).

  2. Level 2: the search range is indices 3 to 4: 99, 899. The middle index is 3, so mid = 99. Since 899 is greater than 99, the search recurses into the right half (index 4 to 4).

  3. Level 3: the search range is index 4 to 4: 899. The middle index is 4, so mid = 899, which equals the key. The search stops here, so the key is found at this level.

This is consistent with the general worst-case bound for binary search: over n elements, the recursion needs at most ⌊log2n⌋ + 1 calls to locate any element. For n = 5, ⌊log25⌋ + 1 = 2 + 1 = 3, and because 899 is the last and largest element — the worst case for this fixed array — the search takes exactly this many levels, confirming the count independently of the step-by-step trace.

Explore the full course: Coding For Placement

Loading lesson…