Boyer-Moore Majority Vote Algorithm

Algorithms
Easy
Google
59.2K views

Given an array `nums` of size $n$, return the majority element (the element that appears more than $\lfloor n/2 \rfloor$ times). The algorithm should run in $O(n)$ time and $O(1)$ space.

Why Interviewers Ask This

Google asks this to evaluate a candidate's ability to optimize for strict memory constraints while maintaining linear time complexity. It tests logical reasoning, pattern recognition in data streams, and the capacity to discard unnecessary storage by leveraging mathematical properties of majority elements rather than relying on hash maps or sorting.

How to Answer This Question

1. Clarify Constraints: Immediately confirm the definition of 'majority' (more than n/2) and reiterate the O(n) time and O(1) space requirements to show you understand Google's efficiency standards. 2. Propose Brute Force First: Briefly mention sorting or hashing solutions to demonstrate baseline knowledge, then explicitly state why they fail the space constraint. 3. Introduce the Algorithm: Explain Boyer-Moore as a voting system where you maintain a 'candidate' and a 'counter'. 4. Walk Through Logic: Detail the two phases: the first pass eliminates non-majority candidates by cancelling out distinct values, and the second pass (optional if guaranteed) verifies the result. 5. Analyze Complexity: Conclude by confirming the single-pass nature ensures O(n) time and the use of only two variables ensures O(1) space.

Key Points to Cover

  • Explicitly acknowledging the O(1) space constraint before coding
  • Explaining the cancellation logic clearly rather than just memorizing code
  • Demonstrating understanding that the majority element guarantees survival
  • Avoiding brute force solutions like HashMaps or Sorting
  • Verifying the solution handles edge cases like arrays with odd/even lengths

Sample Answer

To solve this efficiently within Google's strict performance constraints, I would implement the Boyer-Moore Majority Vote algorithm. The core challenge is finding an element appearing more than half the times without using extra memory like a hash map. The strategy relies on a simple voting mechanism. We initialize a variable for our 'candidate' and a 'counter' set to zero. As we iterate through the array, if the counter is zero, we adopt the current number as our new candidate. If the current number matches the candidate, we increment the counter; otherwise, we decrement it. This works because every time we encounter a different number, we effectively cancel out one instance of the majority element with a non-majority element. Since the majority element appears more than n/2 times, it will inevitably survive these cancellations. For example, with [2, 2, 1, 1, 1, 2, 2], the algorithm cancels pairs of 1s against 2s until only 2 remains. While some variations require a second pass to verify the count, the problem statement guarantees a majority exists, so the survivor is sufficient. This approach runs in O(n) time since we traverse the list once, and uses O(1) space by storing only two integers, perfectly meeting the requirements.

Common Mistakes to Avoid

  • Attempting to sort the array first, which violates the O(1) space requirement
  • Using a Hash Map to count frequencies, which increases space complexity to O(n)
  • Failing to explain the mathematical intuition behind why the counter resets
  • Skipping the verification step when the problem does not guarantee a majority exists

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 87 Google questions