Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Tests fail when concatenating nested array in JavaScript

Implemented a Radix Sort which sorts a list of numbers. Here is my code:

function getDigit(number, index, lengthOfLongestNumber) {
  let numberString = number.toString();

  numberString = numberString.padStart(lengthOfLongestNumber, '0');

  return parseInt(numberString[index], 10) || 0;
}

function getLengthOfLongestNumber(numbers) {
  // return Math.floor(Math.log10(Math.max(...numbers))) + 1;

  let largestNumber = 0;

  for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] > largestNumber) {
      largestNumber = numbers[i];
    }
  }

  return largestNumber.toString().length;
}

function radixSort(numbers) {
  const lengthOfLongestNumber = getLengthOfLongestNumber(numbers);

  for (let i = lengthOfLongestNumber - 1; i >= 0; i--) {
    const buckets = new Array(10).fill().map(() => []);

    while (numbers.length) {
      const number = numbers.shift();

      buckets[getDigit(number, i, lengthOfLongestNumber)].push(number);
    }

    for (let j = 0; j < 10; j++) {
      // numbers = numbers.concat(buckets[j]);  ---> uncomment this line and comment out the following while loop
      // numbers = [...numbers, ...buckets[j]]; ---> or uncomment this line and comment out the following while loop
      while (buckets[j].length) {
        numbers.push(buckets[j].shift());
      }
    }
  }

  return numbers;
}

Tests fail when I merge buckets array into numbers array using concat() in radixSort function (inside inner for loop). But it somehow passes if I use while loop instead.

You can see and run tests in CodeSandbox.

Why is this happening? What is the difference between them that causes tests fail?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For the returned array it doesn't matter which of those alternatives you use, but if the tests expect you to sort the given array, so that after the call the input array has been sorted itself, then indeed, the commented-out alternatives will not work. This is because those alternatives assign to numbers instead of mutating it.

Just check the difference between the alternatives when ignoring the returned array, but looking at the given array:

let arr = [521, 219, 100, 422, 889, 529, 491, 777, 641, 882, 229];
radixSort(arr);
console.log(arr);

The commented-out alternatives will log an empty array.

The correct version mutates numbers, and does not assign a (new) array to it.

To avoid the loop, you can do this mutation also like this:

numbers.push(...buckets[j]);

And you can even replace the for loop around that statement, with just this line:

numbers.push(...buckets.flat());
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda