Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Find the majority element (appearing more than half the time) of [2, 2, 1, 1, 1, 2, 2] using the Boyer–Moore voting algorithm, and report it.

  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

Boyer–Moore keeps a candidate and a counter that is cancelled by different elements; the surviving candidate is the majority if one exists. Choosing the first element without the counter gives the wrong answer here.

More Coding Interview Problems questions

Exams that ask this topic