Code Lab

Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado

Third largest number

MediaArrays10 min

Write a function thirdLargest(numbers) that returns the third largest element of an array of numbers.

  • numbers is an array of integers. They can be positive, negative or 0.
  • All numbers in the array are different.
  • Return the third largest number: in [10, 40, 20, 30] it is 20.
  • If the array has fewer than 3 numbers (also for an empty array), return null.

The interviewer asked you not to use built-in functions such as sort or Math.max: walk through the array with a loop. A hidden test checks this rule: there sort, toSorted, Math.max and Math.min throw an error.

Ejemplos

thirdLargest([10, 40, 20, 30])

Devuelve 20

thirdLargest([3, 1, 2])

Devuelve 1

thirdLargest([5])

Devuelve null

Preguntada en MakeMyTrip · Fuente

Cargando el editor…
  1. EntradathirdLargest([ 10, 40, 20, 30 ])
    Esperado20
  2. EntradathirdLargest([ 3, 1, 2 ])
    Esperado1
  3. EntradathirdLargest([ 5 ])
    Esperadonull

Las pruebas ocultas se ejecutan al enviar