0
0
DSA C++programming~20 mins

Counting Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Counting Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Counting Sort on a Small Array
What is the output of the following C++ code that uses counting sort on the given array?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void countingSort(vector<int>& arr) {
    int max_val = *max_element(arr.begin(), arr.end());
    vector<int> count(max_val + 1, 0);
    for (int num : arr) {
        count[num]++;
    }
    int index = 0;
    for (int i = 0; i <= max_val; i++) {
        while (count[i] > 0) {
            arr[index++] = i;
            count[i]--;
        }
    }
}

int main() {
    vector<int> data = {4, 2, 2, 8, 3, 3, 1};
    countingSort(data);
    for (int num : data) {
        cout << num << " ";
    }
    return 0;
}
A1 2 3 4 8
B8 4 3 3 2 2 1
C1 2 2 3 3 4 8
D1 1 2 2 3 3 4 8
Attempts:
2 left
💡 Hint
Counting sort counts how many times each number appears and then reconstructs the array in order.
🧠 Conceptual
intermediate
1:30remaining
Counting Sort Stability Property
Which statement correctly describes the stability property of counting sort?
ACounting sort is stable because it preserves the relative order of equal elements.
BCounting sort is unstable because it rearranges equal elements arbitrarily.
CCounting sort is stable only if the input array is already sorted.
DCounting sort is unstable because it uses a hash map internally.
Attempts:
2 left
💡 Hint
Stability means equal elements keep their original order after sorting.
Predict Output
advanced
2:30remaining
Counting Sort with Negative Numbers
What will be the output of this modified counting sort code that attempts to sort an array with negative numbers?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void countingSort(vector<int>& arr) {
    int min_val = *min_element(arr.begin(), arr.end());
    int max_val = *max_element(arr.begin(), arr.end());
    int range = max_val - min_val + 1;
    vector<int> count(range, 0);
    for (int num : arr) {
        count[num - min_val]++;
    }
    int index = 0;
    for (int i = 0; i < range; i++) {
        while (count[i] > 0) {
            arr[index++] = i + min_val;
            count[i]--;
        }
    }
}

int main() {
    vector<int> data = {-5, -10, 0, -3, 8, 5, -1, 10};
    countingSort(data);
    for (int num : data) {
        cout << num << " ";
    }
    return 0;
}
A-10 -5 -3 -1 0 5 8 10
B-5 -10 -3 -1 0 5 8 10
C-10 -5 -3 -1 0 5 8
D0 5 8 10 -10 -5 -3 -1
Attempts:
2 left
💡 Hint
Adjusting counts by minimum value allows sorting negative numbers.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Counting Sort Implementation
What error will occur when running this counting sort code snippet?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void countingSort(vector<int>& arr) {
    int max_val = *max_element(arr.begin(), arr.end());
    vector<int> count(max_val, 0);
    for (int num : arr) {
        count[num]++;
    }
    int index = 0;
    for (int i = 0; i <= max_val; i++) {
        while (count[i] > 0) {
            arr[index++] = i;
            count[i]--;
        }
    }
}

int main() {
    vector<int> data = {1, 4, 1, 2, 7, 5, 2};
    countingSort(data);
    for (int num : data) {
        cout << num << " ";
    }
    return 0;
}
AOutput: 1 1 2 2 4 5 7
BRuntime error: out_of_range exception
CCompilation error: missing #include <algorithm>
DOutput: 1 2 4 5 7
Attempts:
2 left
💡 Hint
Check the size of the count vector and the indexing inside the loop.
🚀 Application
expert
3:00remaining
Counting Sort for Sorting Characters by ASCII Values
Given the following code to sort characters by their ASCII values using counting sort, what is the output?
DSA C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void countingSortChars(string& s) {
    int max_val = 127; // ASCII max for standard chars
    vector<int> count(max_val + 1, 0);
    for (char c : s) {
        count[(int)c]++;
    }
    int index = 0;
    for (int i = 0; i <= max_val; i++) {
        while (count[i] > 0) {
            s[index++] = (char)i;
            count[i]--;
        }
    }
}

int main() {
    string text = "dsaAlgorithm123";
    countingSortChars(text);
    cout << text << endl;
    return 0;
}
A
123AGadhilmort
B123AdGahilmort
C123AGadhilmort\n
D123Aadghilmorst
Attempts:
2 left
💡 Hint
Counting sort sorts characters by their ASCII codes in ascending order.