Bird
0
0
DSA Cprogramming~20 mins

Majority Element Moore's Voting Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Moore's Voting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Moore's Voting Algorithm on given array
What is the output of the following C code that implements Moore's Voting Algorithm to find the majority element?
DSA C
#include <stdio.h>

int findMajority(int arr[], int size) {
    int count = 0, candidate = -1;
    for (int i = 0; i < size; i++) {
        if (count == 0) {
            candidate = arr[i];
            count = 1;
        } else if (arr[i] == candidate) {
            count++;
        } else {
            count--;
        }
    }
    return candidate;
}

int main() {
    int arr[] = {2, 2, 1, 1, 1, 2, 2};
    int size = sizeof(arr) / sizeof(arr[0]);
    int majority = findMajority(arr, size);
    printf("%d\n", majority);
    return 0;
}
A2
B1
C0
DNo majority element
Attempts:
2 left
💡 Hint
Remember Moore's Voting Algorithm finds the candidate that may be the majority element by counting occurrences.
Predict Output
intermediate
2:00remaining
Output when no majority element exists
What will the following C code print when the input array has no majority element?
DSA C
#include <stdio.h>

int findMajority(int arr[], int size) {
    int count = 0, candidate = -1;
    for (int i = 0; i < size; 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 < size; i++) {
        if (arr[i] == candidate) count++;
    }
    if (count > size / 2) return candidate;
    else return -1;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    int majority = findMajority(arr, size);
    printf("%d\n", majority);
    return 0;
}
A-1
B1
C5
D0
Attempts:
2 left
💡 Hint
The code verifies if the candidate appears more than half the time before returning it.
🧠 Conceptual
advanced
2:00remaining
Why Moore's Voting Algorithm works
Which of the following best explains why Moore's Voting Algorithm correctly finds the majority element?
ABecause it sorts the array and picks the middle element.
BBecause it uses recursion to divide and conquer the array.
CBecause it counts the frequency of all elements using extra space.
DBecause it cancels out pairs of different elements, leaving the majority element as the candidate.
Attempts:
2 left
💡 Hint
Think about how the algorithm reduces the count when encountering different elements.
🔧 Debug
advanced
2:00remaining
Identify the bug in Moore's Voting Algorithm implementation
What is the bug in the following C code implementing Moore's Voting Algorithm?
DSA C
#include <stdio.h>

int findMajority(int arr[], int size) {
    int count = 0, candidate = 0;
    for (int i = 0; i < size; i++) {
        if (count == 0) {
            candidate = arr[i];
            count = 1;
        } else if (arr[i] != candidate) {
            count++;
        } else {
            count--;
        }
    }
    return candidate;
}

int main() {
    int arr[] = {3, 3, 4, 2, 4, 4, 2, 4, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    int majority = findMajority(arr, size);
    printf("%d\n", majority);
    return 0;
}
AThe candidate is not initialized to -1.
BThe count increments when element is different instead of decrementing.
CThe array size calculation is incorrect.
DThe function does not verify the candidate after selection.
Attempts:
2 left
💡 Hint
Check the condition that changes the count variable inside the loop.
🚀 Application
expert
3:00remaining
Majority Element in a Stream of Data
You receive a stream of integers one by one and want to find the majority element at any point using Moore's Voting Algorithm. Which approach below correctly maintains the majority candidate and count as new elements arrive?
AStore all elements in an array and run Moore's Voting Algorithm from scratch after each new element.
BInitialize candidate to -1 and count to 0. For each new element, always increment count and update candidate to the new element.
CInitialize candidate and count to 0. For each new element, if count is 0, set candidate to element and count to 1; else if element equals candidate, increment count; else decrement count.
DUse a hash map to count frequencies and update candidate when a frequency exceeds half the total elements.
Attempts:
2 left
💡 Hint
Moore's Voting Algorithm can be adapted to streams by updating candidate and count incrementally.