Code Lab

Third largest number

MediumArrays10 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. The tests check only the result; keep to the rule yourself.

Examples

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

Returns 20

thirdLargest([3, 1, 2])

Returns 1

thirdLargest([5])

Returns null

Asked at MakeMyTrip · Source

Loading the editor...
  1. InputthirdLargest([ 10, 40, 20, 30 ])
    Expected20
  2. InputthirdLargest([ 3, 1, 2 ])
    Expected1
  3. InputthirdLargest([ 5 ])
    Expectednull

Hidden tests run on Submit.