Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the count array with zeros.
DSA Javascript
const count = new Array(max + 1).fill([1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes incorrect counts.
Using null or undefined causes errors when incrementing.
✗ Incorrect
Counting sort uses a count array initialized with zeros to store frequencies.
2fill in blank
mediumComplete the code to count the frequency of each element in the input array.
DSA Javascript
for (let i = 0; i < arr.length; i++) { count[arr[i]] [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- decreases the count incorrectly.
Using = overwrites the count instead of incrementing.
✗ Incorrect
We increment the count for each element found in the input array.
3fill in blank
hardFix the error in the code to compute the cumulative count correctly.
DSA Javascript
for (let i = 1; i <= max; i++) { count[i] = count[i] [1] count[i - 1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction causes wrong cumulative counts.
Using multiplication or division is incorrect here.
✗ Incorrect
Cumulative count is the sum of current count and previous counts.
4fill in blank
hardFill both blanks to place elements in the output array in stable order.
DSA Javascript
for (let i = arr.length - 1; i >= 0; i--) { output[count[arr[i]] [1]] = arr[i]; count[arr[i]] [2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not subtracting 1 causes index errors.
Incrementing count instead of decrementing breaks stability.
✗ Incorrect
We place the element at index count[element] - 1 and then decrement the count.
5fill in blank
hardFill all three blanks to copy the sorted output back to the original array.
DSA Javascript
for (let i = 0; i < arr.length; ) { arr[[1]] = output[[2]]; [3]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices causes wrong copying.
Forgetting to increment i causes infinite loop.
✗ Incorrect
We copy each element from output to arr using index i and increment i.