Skip to content
CodeKruPro

Longest run of consecutive numbers

Coding problemMediumAsked in: Amazon

Given an unsorted list of integers, return the length of the longest run of consecutive whole numbers (such as 5, 6, 7, 8) that can be formed from its values. The run's numbers can appear anywhere in the list, in any order.

Repeated values count once. Negative numbers are allowed. An empty list gives 0.

Aim for a solution that does not sort: the largest hidden tests have 100,000 values.

Examples

Example 1
Input: nums = [100,4,200,1,3,2]
Output: 4
The run is 1, 2, 3, 4.
Example 2
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
The run is 0 to 8; the second 0 counts once.
Example 3
Input: nums = []
Output: 0

Constraints

  • The list holds between 0 and 100,000 integers
  • Each value is between -1,000,000,000 and 1,000,000,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in