Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard
The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [9, 6, 6, 6, 6, 5, 6]. What candidate and final counter does it return, and is that candidate really a majority (> n/2)?
- A. candidate 6, counter 4; yes
- B. candidate 6, counter 3; no
- C. candidate 9, counter 3; yes
- D. candidate 6, counter 3; yes
Correct answer: D. candidate 6, counter 3; yes
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 6 with counter 3. Checking by counting shows 6 occurs 5 times out of 7, so it is 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…
- What does this loop-counting code print? (Python) python n, c = 22, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- Kadane's algorithm runs on the array [6, 9, 2, 7, -4, -9, -5, 1, 3, 4] to find the maximum sum of a contiguous subarray. What sum does it…
- 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 of fixed size with a character-frequency count, how many substrings of "BABAAABCC" are anagrams of "AB"?
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [0, 2, 2, 2, 1, 1, 2, 2, 0, 1] (values 0, 1, 2…