Code Lab

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

Largest product of two neighbors

MoyenArrays10 min

Write a function maxAdjacentProduct(numbers) that returns the largest product of two neighbor elements of an array.

  • numbers is an array of integers. They can be positive, negative or 0.
  • Two elements are neighbors when their indexes are i and i + 1. Multiply every such pair and return the largest product as a number.
  • Two negative numbers give a positive product: -5 * -6 is 30.
  • All products can be negative. Then return the largest of them, for example -2.
  • If the array has fewer than two elements, there are no pairs: return null.

Exemples

maxAdjacentProduct([3, 6, -2, -5, 7, 3])

Renvoie 21

maxAdjacentProduct([-5, -6, 1])

Renvoie 30

maxAdjacentProduct([5])

Renvoie null

Posé chez Amazon · Source

Chargement de l'éditeur...
  1. EntréemaxAdjacentProduct([ 3, 6, -2, -5, 7, 3 ])
    Attendu21
  2. EntréemaxAdjacentProduct([ 1, 2, 3 ])
    Attendu6
  3. EntréemaxAdjacentProduct([ 5 ])
    Attendunull

Les tests cachés se lancent à la soumission