Data Structures & AlgorithmsHardTCS Digital · Infosys SP
A peak is an element larger than its neighbours (array ends count as −∞). Binary search runs on [43, 46, 49, 14, 48, 18, 24, 47, 28]: 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. 7
- B. 3
- C. 2
- D. 4
Correct answer: C. 2
The search always moves towards a rising neighbour, so it ends on a local peak — index 2 (value 49). Peaks here: 2, 4, 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
- A fixed window of length 3 slides over s = "bcaacbccbbbbac" with a character-count array. At how many starting positions is the window an…
- For each element of [16, 4, 2, 19, 10, 6, 29, 9], find the next greater element to its RIGHT (−1 if none), using a monotonic stack. What…
- How many distinct non-empty substrings does the string "aaaaa" have, and how many substrings counted by position?
- Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed? python from collections import…
- A string of length n is searched for a pattern of length m. What are the worst-case times of the naive algorithm and of KMP?
- How many inversions (pairs i < j with a[i] > a[j]) does [35, 33, 31, 3, 20, 18, 13, 4] have? (This is also the number of swaps bubble sort…