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 a given array
What is the output of the following Go code that implements Moore's Voting Algorithm to find the majority element?
DSA Go
package main import "fmt" func majorityElement(nums []int) int { count := 0 candidate := 0 for _, num := range nums { if count == 0 { candidate = num } if num == candidate { count++ } else { count-- } } return candidate } func main() { arr := []int{2, 2, 1, 1, 1, 2, 2} fmt.Println(majorityElement(arr)) }
Attempts:
2 left
💡 Hint
The majority element is the one that appears more than half the time.
✗ Incorrect
The algorithm picks candidate 2 and counts its occurrences. Since 2 appears 4 times out of 7, it is the majority element.
🧠 Conceptual
intermediateUnderstanding the count variable in Moore's Voting Algorithm
In Moore's Voting Algorithm, what does the 'count' variable represent during the iteration over the array?
Attempts:
2 left
💡 Hint
Think about how the algorithm balances candidate votes.
✗ Incorrect
The count increases when the current element matches the candidate and decreases otherwise, tracking the net support for the candidate.
🔧 Debug
advancedIdentify the error in this Moore's Voting Algorithm implementation
What error will this Go code produce when run, and why?
DSA Go
package main import "fmt" func majorityElement(nums []int) int { count := 0 candidate := 0 for _, num := range nums { if count == 0 { candidate = num } if num == candidate { count++ } else { count-- } } return candidate } func main() { arr := []int{3, 3, 4} fmt.Println(majorityElement(arr)) }
Attempts:
2 left
💡 Hint
Check the if condition syntax carefully.
✗ Incorrect
The line 'if count = 0' uses assignment '=' instead of comparison '==', causing a syntax error.
🚀 Application
advancedUsing Moore's Voting Algorithm to find majority element in a stream
You receive a stream of integers one by one. How can Moore's Voting Algorithm be adapted to find the majority element without storing the entire stream?
Attempts:
2 left
💡 Hint
Moore's Voting Algorithm is designed for one-pass processing.
✗ Incorrect
The algorithm maintains a candidate and count incrementally, so it works well for streams without storing all data.
❓ Predict Output
expertOutput of Moore's Voting Algorithm on array with no majority element
What is the output of this Go code using Moore's Voting Algorithm on an array with no majority element?
DSA Go
package main import "fmt" func majorityElement(nums []int) int { count := 0 candidate := 0 for _, num := range nums { if count == 0 { candidate = num } if num == candidate { count++ } else { count-- } } return candidate } func main() { arr := []int{1, 2, 3, 4, 5} fmt.Println(majorityElement(arr)) }
Attempts:
2 left
💡 Hint
Moore's Voting Algorithm returns a candidate but does not guarantee majority.
✗ Incorrect
The algorithm returns 5 as candidate after processing, but since no element appears more than half, it's not a true majority.
