Data Structures & AlgorithmsHardGATE CS · DSA
Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many array probes are made in the doubling phase, and what index is returned?
Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many array probes are made in the doubling phase, and what index is returned?
import bisect
a, key = [23, 30, 54, 69, 74, 80, 84, 90, 119, 126, 127, 134, 142, 153, 154, 156, 159, 161, 179, 194], 159
i, probes = 1, 0
while i < len(a):
probes += 1
if a[i] >= key: break
i *= 2
lo, hi = i // 2, min(i, len(a) - 1)
idx = bisect.bisect_left(a, key, lo, hi + 1)
print(probes, idx if idx < len(a) and a[idx] == key else -1)- A. 7 16
- B. 9 16
- C. 5 16
- D. 4 16
Correct answer: C. 5 16
Doubling finds a range of size about the key's position in O(log pos) probes, then binary search finishes it. Output: 5 16.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A Python 2-D list is created two ways and one cell is set. What is printed? python a = [[0] 3] 3 b = [[0] 3 for in range(3)] a[0][0] = 5…
- A circular queue in an array of size 7 keeps one slot empty to tell full from empty (front = rear means empty; (rear + 1) mod 7 = front…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [4…
- What is the minimum number of stacks needed to evaluate a fully parenthesised infix expression directly (without converting it first)…
- Merge sort and quick sort are compared. Which statement is correct?
- Bars of width 1 have heights [7, 3, 3, 6, 1, 3, 7, 1, 1]. How many units of rain water are trapped between them (two-pointer method with…