Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
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 = [60, 62, 65, 75, 78, 88, 94, 95, 108, 109, 111, 115, 125, 138, 140, 146, 154, 183, 190, 195], 183
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. 9 17
- B. 6 17
- C. 5 17
- D. 7 17
Correct answer: C. 5 17
Doubling finds a range of size about the key's position in O(log pos) probes, then binary search finishes it. Output: 5 17.
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
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- Binary search with mid = (lo + hi) // 2 (lo = 0, hi = n − 1, loop while lo ≤ hi) looks for 16 in [18, 39, 40, 44, 45, 56, 60, 62, 70, 84…
- For the sorted array [3, 3, 4, 5, 5, 7, 7, 8, 8, 8, 8, 10] (0-based), what are lowerbound(5) (first index with value ≥ 5) and…
- How many contiguous subarrays of [−2, 1, −3, −4, 8, 1, 4, −5, 6] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- What does this loop-counting code print? (Python) python n, c = 16, 0 i = 2 while i <= n: c += 1 i = i i print(c)