Longest Streak of Consecutive Login Days by a Single User

Quick Overview

Given unordered login records of user ids and calendar dates, return the longest run of consecutive days on which a single user logged in, counting repeat logins on the same day once. Tests grouping by user, date arithmetic across month, year and leap-day boundaries, and counting runs that reset after a gap.

Longest Streak of Consecutive Login Days by a Single User

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given login records, each a user id and the calendar date of one login. Return the length of the longest streak of consecutive days on which a single user logged in. A streak ends as soon as that user's next login date is more than one day after the previous one; counting then starts again. The candidate's report does not say whether the answer is reported per user or overall; this version returns the longest streak achieved by any single user. ### Function Signature ```python def longest_login_streak(logins: list[list[str]]) -> int: ``` ### Rules - Each record is `[user_id, date]`, where `date` is a valid Gregorian calendar date in `YYYY-MM-DD` format. - Records are in no particular order, and a user may log in several times on the same day; those logins count as one day. - A streak is a maximal run of consecutive calendar days on each of which one user logged in at least once. Consecutive days cross month and year boundaries normally, and February 29 is a real day in leap years. - A streak belongs to one user: logins by other users never extend or break it. - Return the length, in days, of the longest streak over all users. ### Constraints - `1 <= len(logins) <= 10^5` - `user_id` has 1 to 20 characters, each a lowercase English letter or a digit. - Every date lies between `2000-01-01` and `2099-12-31`. ### Examples **Example 1** ```text Input: logins = [["u1", "2026-05-03"], ["u2", "2026-05-03"], ["u1", "2026-05-04"], ["u1", "2026-05-04"], ["u1", "2026-05-06"], ["u2", "2026-05-04"], ["u2", "2026-05-05"]] Output: 3 ``` User `u1` logged in on May 3, May 4 (twice) and May 6, giving streaks of 2 and 1 days. User `u2` logged in on May 3, 4 and 5, a streak of 3 days. **Example 2** ```text Input: logins = [["a", "2024-03-01"], ["a", "2024-02-28"], ["a", "2024-02-29"], ["b", "2024-03-02"]] Output: 3 ``` User `a` logged in on February 28, February 29 (2024 is a leap year) and March 1, a streak of 3 days. User `b`'s login on March 2 does not extend `a`'s streak. **Example 3** ```text Input: logins = [["z", "2025-12-31"], ["z", "2026-01-02"]] Output: 1 ``` January 1 is missing, so the two logins form two separate streaks of 1 day.

Overview: Given unordered login records of user ids and calendar dates, return the longest run of consecutive days on which a single user logged in, counting repeat logins on the same day once. Tests grouping by user, date arithmetic across month, year and leap-day boundaries, and counting runs that reset after a gap.

Amazon logo
Amazon
Oct 24, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

You are given login records, each a user id and the calendar date of one login. Return the length of the longest streak of consecutive days on which a single user logged in. A streak ends as soon as that user's next login date is more than one day after the previous one; counting then starts again.

The candidate's report does not say whether the answer is reported per user or overall; this version returns the longest streak achieved by any single user.

Function Signature

def longest_login_streak(logins: list[list[str]]) -> int:

Rules

  • Each record is [user_id, date] , where date is a valid Gregorian calendar date in YYYY-MM-DD format.
  • Records are in no particular order, and a user may log in several times on the same day; those logins count as one day.
  • A streak is a maximal run of consecutive calendar days on each of which one user logged in at least once. Consecutive days cross month and year boundaries normally, and February 29 is a real day in leap years.
  • A streak belongs to one user: logins by other users never extend or break it.
  • Return the length, in days, of the longest streak over all users.

Constraints

  • 1 <= len(logins) <= 10^5
  • user_id has 1 to 20 characters, each a lowercase English letter or a digit.
  • Every date lies between 2000-01-01 and 2099-12-31 .

Examples

Example 1

Input:  logins = [["u1", "2026-05-03"], ["u2", "2026-05-03"], ["u1", "2026-05-04"], ["u1", "2026-05-04"], ["u1", "2026-05-06"], ["u2", "2026-05-04"], ["u2", "2026-05-05"]]
Output: 3

User u1 logged in on May 3, May 4 (twice) and May 6, giving streaks of 2 and 1 days. User u2 logged in on May 3, 4 and 5, a streak of 3 days.

Example 2

Input:  logins = [["a", "2024-03-01"], ["a", "2024-02-28"], ["a", "2024-02-29"], ["b", "2024-03-02"]]
Output: 3

User a logged in on February 28, February 29 (2024 is a leap year) and March 1, a streak of 3 days. User b's login on March 2 does not extend a's streak.

Example 3

Input:  logins = [["z", "2025-12-31"], ["z", "2026-01-02"]]
Output: 1

January 1 is missing, so the two logins form two separate streaks of 1 day.