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)?

  1. A. candidate 4, counter 0; no
  2. B. candidate 6, counter 0; no
  3. C. candidate 4, counter 0; yes
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic