Data Structures & AlgorithmsHardHCLTech · Wipro pattern
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 index, last index) pair (0-based) does it return?
- A. (7, 7)
- B. (7, 9)
- C. (6, 8)
- D. (7, 8)
Correct answer: D. (7, 8)
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 7 and last index 8.
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
- 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…
- Two sorted linked lists [12 → 17 → 24 → 26 → 32] and [8 → 10 → 12 → 38] are merged with the standard O(n+m) two-pointer merge (always take…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 1, 2, 2, 1, 1, 1, 1, 2, 0] (values 0, 1, 2…
- Heights [8, 6, 3, 2, 6, 8, 8, 4, 7, 1] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…