Longest trip without repeat cities

two pointers

Longest trip without repeat cities

Airbnb Python Interview Question

Airbnb's travel insights team stores a guest's booking history as a string with one letter per city, in the order the guest stayed. They want the longest run of back-to-back stays that never returns to a city.

Write a function longest_unique_streak(cities) that returns the length of the longest stretch of consecutive letters with no letter repeated.

Asked of

  • Data Engineer
  • Data Scientist
  • Analytics Engineer
  • ML Engineer
  • AI Engineer

Example 1

Input

longest_unique_streak("abcabcbb")

Output

3

Example 2

Input

longest_unique_streak("bbbb")

Output

1

Explanation

In the first example, "abc" is the longest stretch with no repeated city: each longer stretch, such as "abca", returns to a city. In the second example, every stay is in the same city, so the longest stretch is 1.

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