Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Binary search for first/last occurrence runs on the sorted array [5, 6, 7, 7, 15, 18, 19, 20] for target 7. What (first index, last index) pair (0-based) does it return?
- A. (2, 3)
- B. (2, 2)
- C. (1, 3)
- D. (2, 4)
Correct answer: A. (2, 3)
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 7 in this array that gives first index 2 and last index 3.
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 (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- A singly linked list holds 36 -> 38 -> 51 -> 57 -> 28 -> None (in that order, 5 nodes). Using the standard two-pointer technique (advance…
- Evaluate the postfix expression: 4 3 7 + 1 -
- A circular queue is implemented in an array of size 9 with front = 1 and rear = 8 (rear indexes the last stored element, the queue is…
- Quicksort with Lomuto partitioning (last element as pivot) sorts [12, 42, 92, 73, 37, 39, 75, 75, 32]. How many swaps happen in total…
- A singly linked list is 95 -> 83 -> 36 -> 42 -> 99 -> 64 -> 72 -> None. The first 5 nodes are reversed in place (the node that was…