Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [6, 4, 4, 4, 6, 4, 6, 2]. What candidate and final counter does it return, and is that candidate really a majority (> n/2)?
- A. candidate 4, counter 0; no
- B. candidate 6, counter 0; no
- C. candidate 4, counter 0; yes
- D. candidate 4, counter 1; no
Correct answer: A. candidate 4, counter 0; no
Tracing the vote (increment the counter on a match with the candidate, decrement on a mismatch, replace the candidate when the counter hits 0) leaves candidate 4 with counter 0. Checking by counting shows 4 occurs 4 times out of 8, so it is not a true majority — the algorithm must always be verified with a second pass.
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 min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-3)…
- Heights [3, 6, 1, 4, 7, 9, 3, 7, 7, 4] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Binary search for first/last occurrence runs on the sorted array [8, 9, 15, 18, 18, 20, 21, 22] for target 18. What (first index, last…
- What is the sum of the minimum element over every contiguous subarray of [4, 1, 1, 2, 3, 3, 7, 3]?
- What does this loop-counting code print? (Python) python n, c = 42, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)