Data Structures & AlgorithmsVery hardProduct company OA

An extended Boyer–Moore vote keeps two candidates to find every value occurring MORE than n/3 times, then verifies them with a second pass. For [5, 1, 5, 2, 1, 5, 3, 1, 1], which values are reported?

  1. A. 1 only
  2. B. 5 and 1
  3. C. 5 only
  4. D. none

Correct answer: A. 1 only

n = 9, so a value must appear more than 3 times. 1 appears 4 times and 5 appears 3 times, so only 1 qualifies even though both survive as candidates. That is why the verification pass is required; skipping it reports 5 as well.

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

Exams that ask this topic