0
0
DSA Javascriptprogramming~10 mins

Radix Sort Algorithm in DSA Javascript - Interactive Practice

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

Complete the code to get the digit at a specific place value.

DSA Javascript
function getDigit(num, place) {
  return Math.floor(Math.abs(num) / [1]) % 10;
}
Drag options to blanks, or click blank then click option'
A10 ** place
Bplace * 10
Cplace + 10
D10 * place
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of exponentiation for place value.
Not using Math.floor to remove decimals.
2fill in blank
medium

Complete the code to count the number of digits in a number.

DSA Javascript
function digitCount(num) {
  if (num === 0) return 1;
  return Math.floor(Math.log10(Math.abs(num))) + [1];
}
Drag options to blanks, or click blank then click option'
A0
B1
C10
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 1 after log10.
Not handling zero correctly.
3fill in blank
hard

Fix the error in the code to find the maximum number of digits in an array.

DSA Javascript
function mostDigits(nums) {
  let maxDigits = 0;
  for (let i = 0; i < nums.length; i++) {
    maxDigits = Math.max(maxDigits, [1](nums[i]));
  }
  return maxDigits;
}
Drag options to blanks, or click blank then click option'
AMath.abs
BgetDigit
CMath.log10
DdigitCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using getDigit which returns a single digit, not count.
Using Math.log10 directly without floor or handling zero.
4fill in blank
hard

Fill both blanks to correctly place numbers into buckets by digit and flatten the array.

DSA Javascript
for (let k = 0; k < maxDigitCount; k++) {
  let buckets = Array.from({ length: 10 }, () => []);
  for (let i = 0; i < nums.length; i++) {
    let digit = [1](nums[i], k);
    buckets[digit].[2](nums[i]);
  }
  nums = [].concat(...buckets);
}
Drag options to blanks, or click blank then click option'
AgetDigit
Bpush
Cpop
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop or shift which remove elements instead of adding.
Using digitCount instead of getDigit here.
5fill in blank
hard

Fill all three blanks to complete the radix sort function.

DSA Javascript
function radixSort(nums) {
  let maxDigitCount = [1](nums);
  for (let k = 0; k < maxDigitCount; k++) {
    let buckets = Array.from({ length: 10 }, () => []);
    for (let i = 0; i < nums.length; i++) {
      let digit = [2](nums[i], k);
      buckets[digit].push(nums[i]);
    }
    nums = [].concat(...buckets);
  }
  return nums;
}

// Helper functions
function digitCount(num) {
  if (num === 0) return 1;
  return Math.floor(Math.log10(Math.abs(num))) + 1;
}

function mostDigits(nums) {
  let maxDigits = 0;
  for (let i = 0; i < nums.length; i++) {
    maxDigits = Math.max(maxDigits, digitCount(nums[i]));
  }
  return maxDigits;
}

function getDigit(num, place) {
  return Math.floor(Math.abs(num) / (10 ** place)) % 10;
}
Drag options to blanks, or click blank then click option'
AmostDigits
BgetDigit
CdigitCount
DMath.max
Attempts:
3 left
💡 Hint
Common Mistakes
Using digitCount instead of mostDigits for maxDigitCount.
Using digitCount inside the loop instead of getDigit.