Data Structures & AlgorithmsHardProduct-company MCQ pattern
Binary search for first/last occurrence runs on the sorted array [2, 5, 6, 16, 19, 19, 20, 21] for target 19. What (first index, last index) pair (0-based) does it return?
- A. (3, 5)
- B. (4, 4)
- C. (4, 6)
- D. (4, 5)
Correct answer: D. (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 19 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
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9] is left-rotated by d = 3 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- Two sorted linked lists [8 → 33 → 35 → 40] and [2 → 13 → 16] are merged with the standard O(n+m) two-pointer merge (always take the…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(2), enqueue(6), dequeue(), dequeue(), enqueue(1)…
- Keys 13, 28, 91, 42, 33 are inserted via linear probing (h(k) = k mod 7, probe by +1 on collision) into an initially empty table of size 7…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…