Data Structures & AlgorithmsHardTCS Digital · Infosys SP
A sorted array was rotated to give [29, 47, 50, 57, 60, 61, 75, 4, 5, 8, 13]. Binary search finds the index of the minimum: while lo < hi, mid = (lo + hi) // 2; if a[mid] > a[hi] then lo = mid + 1 else hi = mid. What index is returned and how many iterations run?
- A. index 7, 4 iterations
- B. index 6, 4 iterations
- C. index 7, 5 iterations
- D. index 8, 4 iterations
Correct answer: A. index 7, 4 iterations
Comparing with a[hi] tells which half is sorted; the minimum 4 is at index 7 (the rotation point), reached after 4 iterations.
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
- Lines of heights [6, 9, 5, 6, 8, 1, 8, 3, 5] stand at x = 0, 1, …, 8. Two pointers find the pair of lines that, with the x-axis, holds the…
- 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…
- 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)…