Code Lab

Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion

Negative numbers to the left

MoyenArrays10 min

Write a function groupNegatives(numbers) that returns a new array with the negative numbers on the left: first all negative numbers, then all other numbers.

  • numbers is an array of integers. It can be empty.
  • 0 is not negative: it goes to the right group, together with the positive numbers.
  • Keep the order inside each group as in numbers. [-5, 10, -3, 8, -1, 6] gives [-5, -3, -1, 10, 8, 6]. Do not sort the numbers.
  • Return a new array. Do not change (do not mutate) numbers.

Each element goes to one of two groups. filter can pick the elements of one group.

Exemples

groupNegatives([-5, 10, -3, 8, -1, 6])

Renvoie [ -5, -3, -1, 10, 8, 6 ]

groupNegatives([4, -7, 0, 2])

Renvoie [ -7, 4, 0, 2 ]

Posé chez ServiceNow · Source

Chargement de l'éditeur...
  1. EntréegroupNegatives([ -5, 10, -3, 8, -1, 6 ])
    Attendu[ -5, -3, -1, 10, 8, 6 ]
  2. EntréegroupNegatives([ 4, -7, 2 ])
    Attendu[ -7, 4, 2 ]

Les tests cachés se lancent à la soumission