Data Structures & AlgorithmsHardGATE CS · Product placement
A peak is an element larger than its neighbours (array ends count as −∞). Binary search runs on [16, 19, 40, 1, 18, 30, 54, 59, 45]: while lo < hi, mid = (lo + hi) // 2; if a[mid] < a[mid + 1] then lo = mid + 1 else hi = mid. Which index does it return?
- A. 2
- B. 4
- C. 8
- D. 7
Correct answer: D. 7
The search always moves towards a rising neighbour, so it ends on a local peak — index 7 (value 59). Peaks here: 2, 7.
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
- The median-of-medians (groups of 5) selection algorithm is used to choose a pivot. What does it guarantee?
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Over all left rotations of [7, 7, 1, 1, 3, 6], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 7, "pop", 3, "pop"…
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…