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 C# code that implements Moore's Voting Algorithm to find the majority element?
DSA C#
using System; class Program { static int FindMajorityElement(int[] nums) { int count = 0, candidate = -1; foreach (int num in nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } static void Main() { int[] arr = {2, 2, 1, 1, 1, 2, 2}; Console.WriteLine(FindMajorityElement(arr)); } }
Attempts:
2 left
💡 Hint
The majority element is the one that appears more than half the time in the array.
✗ Incorrect
The algorithm picks candidate 2 and confirms it as majority because it appears 4 times out of 7, which is more than half.
❓ Predict Output
intermediateResult when no majority element exists
What will the following C# code output when the input array has no majority element?
DSA C#
using System; class Program { static int FindMajorityElement(int[] nums) { int count = 0, candidate = -1; foreach (int num in nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } static void Main() { int[] arr = {1, 2, 3, 4, 5}; Console.WriteLine(FindMajorityElement(arr)); } }
Attempts:
2 left
💡 Hint
The algorithm returns a candidate even if no majority exists.
✗ Incorrect
The algorithm returns 5 as candidate but it is not a majority element because no element appears more than half the time.
🔧 Debug
advancedIdentify the bug in Moore's Voting Algorithm implementation
What error will the following C# code produce when run?
DSA C#
using System; class Program { static int FindMajorityElement(int[] nums) { int count = 0, candidate = 0; foreach (int num in nums) { if (count == 0) { candidate = num; count = 1; } else if (num == candidate) { count++; } else { count--; } } return candidate; } static void Main() { int[] arr = {2, 2, 1, 1, 1, 2, 2}; Console.WriteLine(FindMajorityElement(arr)); } }
Attempts:
2 left
💡 Hint
Check the condition inside the if statement carefully.
✗ Incorrect
The condition uses assignment (=) instead of comparison (==), causing the if body to never execute (condition always false after assignment), so candidate remains 0.
🧠 Conceptual
advancedUnderstanding 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 count changes when you see the candidate or a different element.
✗ Incorrect
Count increases when the current element matches the candidate and decreases otherwise, tracking the net support for the candidate.
🚀 Application
expertApplying Moore's Voting Algorithm to find majority element in a stream
You receive a stream of integers one by one and want to find the majority element at the end using Moore's Voting Algorithm. Which approach correctly updates the candidate and count as each new number arrives?
Attempts:
2 left
💡 Hint
Recall how Moore's Voting Algorithm updates candidate and count during iteration.
✗ Incorrect
Option D correctly describes the update rules of Moore's Voting Algorithm for streaming data.
