Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Third largest number
MoyenArrays10 min
Write a function thirdLargest(numbers) that returns the third largest element of an array of numbers.
numbersis an array of integers. They can be positive, negative or0.- All numbers in the array are different.
- Return the third largest number: in
[10, 40, 20, 30]it is20. - 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.
Exemples
thirdLargest([10, 40, 20, 30])Renvoie 20
thirdLargest([3, 1, 2])Renvoie 1
thirdLargest([5])Renvoie null
Posé chez MakeMyTrip · Source
Chargement de l'éditeur...
- Entrée
thirdLargest([ 10, 40, 20, 30 ])Attendu20 - Entrée
thirdLargest([ 3, 1, 2 ])Attendu1 - Entrée
thirdLargest([ 5 ])Attendunull
Les tests cachés se lancent à la soumission