Challenge - 5 Problems
Moore's Voting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Moore's Voting Algorithm on an array
What is the output of the following JavaScript code that uses Moore's Voting Algorithm to find the majority element?
DSA Javascript
function majorityElement(nums) {
let count = 0;
let candidate = null;
for (const num of nums) {
if (count === 0) {
candidate = num;
}
count += (num === candidate) ? 1 : -1;
}
return candidate;
}
console.log(majorityElement([2,2,1,1,1,2,2]));Attempts:
2 left
💡 Hint
Think about which number appears more than half the time in the array.
✗ Incorrect
The algorithm picks 2 as the candidate because it appears more than half the time (4 out of 7). The count increments and decrements to track the majority candidate.
❓ Predict Output
intermediateResult when no majority element exists
What will the following code output when there is no majority element in the array?
DSA Javascript
function majorityElement(nums) {
let count = 0;
let candidate = null;
for (const num of nums) {
if (count === 0) {
candidate = num;
}
count += (num === candidate) ? 1 : -1;
}
return candidate;
}
console.log(majorityElement([1,2,3,4,5]));Attempts:
2 left
💡 Hint
The algorithm returns a candidate but it may not be a true majority if none exists.
✗ Incorrect
The algorithm returns 5 as candidate because it ends as the last candidate after count adjustments, but no majority element exists in the array.
🧠 Conceptual
advancedWhy Moore's Voting Algorithm works
Which statement best explains why Moore's Voting Algorithm correctly finds the majority element?
Attempts:
2 left
💡 Hint
Think about how the algorithm reduces the problem by canceling out pairs.
✗ Incorrect
The algorithm cancels out pairs of different elements, so the majority element remains because it appears more than half the time.
❓ Predict Output
advancedOutput after verifying candidate is majority
What will the following code output after verifying the candidate is actually the majority element?
DSA Javascript
function majorityElement(nums) {
let count = 0;
let candidate = null;
for (const num of nums) {
if (count === 0) {
candidate = num;
}
count += (num === candidate) ? 1 : -1;
}
// Verify candidate
count = 0;
for (const num of nums) {
if (num === candidate) count++;
}
return count > nums.length / 2 ? candidate : null;
}
console.log(majorityElement([1,2,3,2,2]));Attempts:
2 left
💡 Hint
Check if the candidate appears more than half the time.
✗ Incorrect
The candidate is 2, and it appears 3 times out of 5, which is more than half, so the function returns 2.
🔧 Debug
expertIdentify the bug in Moore's Voting Algorithm implementation
What error will the following code produce when run?
DSA Javascript
function majorityElement(nums) {
let count = 0;
let candidate;
for (const num of nums) {
if (count === 0) {
candidate = num;
}
count += (num === candidate) ? 1 : -1;
}
return candidate;
}
console.log(majorityElement([1,1,2,2,2]));Attempts:
2 left
💡 Hint
Look carefully at the if condition inside the loop.
✗ Incorrect
The condition uses assignment (=) instead of comparison (===), so count is set to 0 every time, causing candidate to be reset incorrectly and returning wrong candidate.
