Longest run of consecutive days

hashing

Longest run of consecutive days

Peloton Python Interview Question

Peloton records the day numbers on which a member worked out, in no particular order and sometimes with repeats.

Write a function longest_consecutive(days) that returns the length of the longest run of consecutive day numbers, such as 4, 5, 6 and 7. Aim to avoid sorting the list.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

days = [100, 4, 200, 1, 3, 2]

Output

4

Example 2

Input

days = [1, 2, 0, 1]

Output

3

Explanation

In the first example, the member worked out on days 1, 2, 3 and 4, a run of 4 consecutive days. Days 100 and 200 stand alone. In the second example, the repeated day 1 still counts once, so the longest run is 0, 1 and 2.

Submit also runs 4 hidden test cases that check edge cases.