Bird
Raised Fist0
DSA Rustprogramming~10 mins

Counting Sort Algorithm in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the count array with zeros.

DSA Rust
let mut count = vec![[1]; max + 1];
Drag options to blanks, or click blank then click option'
Amax
B1
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes wrong counts.
Using max as initial value fills with wrong numbers.
2fill in blank
medium

Complete the code to count each element's frequency.

DSA Rust
for &num in arr.iter() {
    count[[1]] += 1;
}
Drag options to blanks, or click blank then click option'
Anum
Bcount
Cindex
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Using count or arr as index causes errors.
Using an undefined variable like index.
3fill in blank
hard

Fix the error in updating the count array to cumulative counts.

DSA Rust
for i in 1..=max {
    count[i] = count[i] [1] count[i - 1];
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication changes counts incorrectly.
Division is invalid here.
4fill in blank
hard

Fill both blanks to place elements in output array in stable order.

DSA Rust
for &num in arr.iter().rev() {
    output[count[[1]] [2] 1] = num;
    count[num] -= 1;
}
Drag options to blanks, or click blank then click option'
Anum
B-
C+
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 causes index out of range.
Using count or wrong index causes errors.
5fill in blank
hard

Fill all three blanks to copy sorted elements back to original array.

DSA Rust
let mut i = 0;
while i < arr.len() {
    arr[[1]] = output[[2]];
    [3] += 1;
}
Drag options to blanks, or click blank then click option'
Ai
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count instead of i causes wrong copying.
Not incrementing index causes infinite loop.

Practice

(1/5)
1. What is the main idea behind the Counting Sort Algorithm?
easy
A. Compare each element with every other element to sort
B. Count the occurrences of each element and use counts to place elements in order
C. Divide the array into halves and sort recursively
D. Swap adjacent elements if they are in wrong order repeatedly

Solution

  1. Step 1: Understand Counting Sort principle

    Counting sort counts how many times each value appears in the input.
  2. Step 2: Use counts to place elements

    These counts help place elements directly in the sorted output without comparisons.
  3. Final Answer:

    Count the occurrences of each element and use counts to place elements in order -> Option B
  4. Quick Check:

    Counting sort = Counting occurrences and placing [OK]
Hint: Counting sort uses counts, not comparisons, to sort [OK]
Common Mistakes:
  • Thinking counting sort compares elements
  • Confusing counting sort with quicksort or mergesort
  • Assuming counting sort works on all data types
2. Which of the following is the correct Rust syntax to create a vector of zeros with length 5 for counting sort?
easy
A. let counts = vec![0; 5];
B. let counts = vec(0, 5);
C. let counts = [0; 5];
D. let counts = vec::new(5, 0);

Solution

  1. Step 1: Recall Rust vector initialization

    In Rust, vec![value; size] creates a vector with repeated values.
  2. Step 2: Match syntax to create vector of zeros

    vec![0; 5] creates a vector with five zeros, perfect for counting sort.
  3. Final Answer:

    let counts = vec![0; 5]; -> Option A
  4. Quick Check:

    vec![0; 5] creates vector of 5 zeros [OK]
Hint: Use vec![value; size] to create repeated elements vector [OK]
Common Mistakes:
  • Using parentheses instead of square brackets for vec!
  • Confusing array syntax with vector syntax
  • Trying to use vec::new with parameters
3. What will be the output of the following Rust code snippet implementing counting sort on array [4, 2, 2, 8, 3, 3, 1]?
fn counting_sort(arr: &[usize]) -> Vec<usize> {
    let max = *arr.iter().max().unwrap();
    let mut counts = vec![0; max + 1];
    for &num in arr {
        counts[num] += 1;
    }
    let mut sorted = Vec::new();
    for (num, &count) in counts.iter().enumerate() {
        for _ in 0..count {
            sorted.push(num);
        }
    }
    sorted
}

fn main() {
    let arr = [4, 2, 2, 8, 3, 3, 1];
    let sorted = counting_sort(&arr);
    println!("{:?}", sorted);
}
medium
A. [0, 1, 2, 3, 4, 8]
B. [8, 4, 3, 3, 2, 2, 1]
C. [1, 3, 2, 4, 8]
D. [1, 2, 2, 3, 3, 4, 8]

Solution

  1. Step 1: Count occurrences of each number

    Input array: [4, 2, 2, 8, 3, 3, 1]. Counts: 1->1, 2->2, 3->2, 4->1, 8->1.
  2. Step 2: Build sorted vector using counts

    Push each number count times in order: 1, 2, 2, 3, 3, 4, 8.
  3. Final Answer:

    [1, 2, 2, 3, 3, 4, 8] -> Option D
  4. Quick Check:

    Sorted array = [1, 2, 2, 3, 3, 4, 8] [OK]
Hint: Counting sort output is sorted array with repeated elements [OK]
Common Mistakes:
  • Forgetting to repeat elements by their count
  • Outputting original array instead of sorted
  • Miscounting occurrences of elements
4. Identify the bug in this Rust counting sort code snippet and choose the fix:
fn counting_sort(arr: &[usize]) -> Vec<usize> {
    let max = arr.iter().max().unwrap();
    let mut counts = vec![0; *max];
    for &num in arr {
        counts[num] += 1;
    }
    let mut sorted = Vec::new();
    for (num, &count) in counts.iter().enumerate() {
        for _ in 0..count {
            sorted.push(num);
        }
    }
    sorted
}
medium
A. Change let max = arr.iter().max().unwrap(); to let max = *arr.iter().max().unwrap();
B. Change for &num in arr to for num in arr.iter()
C. Change vec![0; *max] to vec![0; *max + 1]
D. Change sorted.push(num) to sorted.push(count)

Solution

  1. Step 1: Check vector size for counts

    Vector size is *max, but max element index is *max, so size must be *max + 1 to include max.
  2. Step 2: Fix counts vector size

    Changing to vec![0; *max + 1] prevents out-of-bound errors when counting max element.
  3. Final Answer:

    Change vec![0; *max] to vec![0; *max + 1] -> Option C
  4. Quick Check:

    Counts vector size = max + 1 [OK]
Hint: Counts vector must cover all indices up to max element [OK]
Common Mistakes:
  • Not adding +1 to vector size for max element
  • Dereferencing max incorrectly
  • Pushing count instead of element in sorted vector
5. You want to sort an array of integers that includes negative numbers using counting sort in Rust. Which approach correctly adapts counting sort for this case?
hard
A. Shift all numbers by adding the absolute value of the minimum negative number before counting
B. Use the original counting sort without changes; it works with negatives
C. Ignore negative numbers and sort only non-negative ones
D. Convert all numbers to positive by multiplying by -1 before sorting

Solution

  1. Step 1: Understand counting sort limitation

    Counting sort requires non-negative indices for counts, so negatives cause index errors.
  2. Step 2: Shift numbers to non-negative range

    Add absolute value of minimum negative number to all elements to shift them to zero or positive before counting.
  3. Step 3: Sort and then shift back

    After sorting, subtract the shift to restore original values in sorted order.
  4. Final Answer:

    Shift all numbers by adding the absolute value of the minimum negative number before counting -> Option A
  5. Quick Check:

    Shift negatives to non-negative before counting sort [OK]
Hint: Shift negatives to zero or positive before counting sort [OK]
Common Mistakes:
  • Trying to use counting sort directly on negatives
  • Ignoring negative numbers in input
  • Multiplying negatives by -1 changes order incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard