Challenge - 5 Problems
Moore's Voting Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Moore's Voting Algorithm on a given array
What is the output of the following C++ code that implements Moore's Voting Algorithm to find the majority element?
DSA C++
int findMajorityElement(int arr[], int n) { int count = 0, candidate = -1; for (int i = 0; i < n; i++) { if (count == 0) { candidate = arr[i]; count = 1; } else if (arr[i] == candidate) { count++; } else { count--; } } // Verify candidate count = 0; for (int i = 0; i < n; i++) { if (arr[i] == candidate) count++; } if (count > n / 2) return candidate; return -1; } int main() { int arr[] = {2, 2, 1, 1, 1, 2, 2}; int n = sizeof(arr) / sizeof(arr[0]); int result = findMajorityElement(arr, n); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember Moore's Voting Algorithm finds the element that appears more than n/2 times if it exists.
✗ Incorrect
The candidate selected by the algorithm is 2, which appears 4 times out of 7, more than half. So the output is 2.
🧠 Conceptual
intermediateUnderstanding the candidate selection in Moore's Voting Algorithm
In Moore's Voting Algorithm, what does the 'candidate' variable represent during the iteration over the array?
Attempts:
2 left
💡 Hint
Think about how the algorithm updates the candidate when count reaches zero.
✗ Incorrect
The candidate is the element currently considered as the potential majority element. It changes when count drops to zero.
🔧 Debug
advancedIdentify the error in this Moore's Voting Algorithm implementation
What error will this code produce when run, and why?
DSA C++
int findMajorityElement(int arr[], int n) { int count = 0, candidate; for (int i = 0; i < n; i++) { if (count == 0) { candidate = arr[i]; count = 1; } else if (arr[i] == candidate) { count++; } else { count--; } } // Missing verification step return candidate; } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int result = findMajorityElement(arr, n); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the candidate is verified before returning.
✗ Incorrect
The code returns the candidate without verifying if it appears more than n/2 times, so it may return a wrong element.
❓ Predict Output
advancedOutput of Moore's Voting Algorithm on an array with no majority
What is the output of this code when the input array has no majority element?
DSA C++
int findMajorityElement(int arr[], int n) { int count = 0, candidate = -1; for (int i = 0; i < n; i++) { if (count == 0) { candidate = arr[i]; count = 1; } else if (arr[i] == candidate) { count++; } else { count--; } } count = 0; for (int i = 0; i < n; i++) { if (arr[i] == candidate) count++; } if (count > n / 2) return candidate; return -1; } int main() { int arr[] = {1, 2, 3, 4}; int n = sizeof(arr) / sizeof(arr[0]); int result = findMajorityElement(arr, n); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
If no element appears more than half the time, the function returns -1.
✗ Incorrect
No element appears more than n/2 times, so the function returns -1.
🚀 Application
expertNumber of times the candidate changes in Moore's Voting Algorithm
Given the array [3, 3, 4, 2, 4, 4, 2, 4, 4], how many times does the candidate variable change its value during the execution of Moore's Voting Algorithm?
Attempts:
2 left
💡 Hint
Track candidate changes when count resets to zero.
✗ Incorrect
Candidate changes occur at indices 0 (3) and 4 (4), totaling 2 changes.
