Data Structures & AlgorithmsVery hardProduct-company MCQ pattern
Binary search for first/last occurrence runs on the sorted array [2, 7, 9, 11, 16, 16, 17, 18] for target 16. What (first index, last index) pair (0-based) does it return?
- A. (4, 4)
- B. (4, 5)
- C. (3, 5)
- D. (4, 6)
Correct answer: B. (4, 5)
The first-occurrence search keeps moving left after a match (hi = mid − 1) to find the earliest index, and the last-occurrence search keeps moving right (lo = mid + 1) to find the latest; for 16 in this array that gives first index 4 and last index 5.
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 recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(6)?
- A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-4), pop()…
- Binary search for first/last occurrence runs on the sorted array [1, 5, 6, 7, 10, 13, 18, 19, 19, 20, 21] for target 19. What (first…
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "CBCAACACBABCB" are anagrams of "CAB"?
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [1, 3, 3, 1, 1, 3, 3, 3]. What candidate and…
- Array [90, 6, 26, 27, 39, 47, 79] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…