Login streak board
MediumOnline Assessment
A language-learning app shows a streak board: for each learner, how many days in a row they have practised. Work it out from raw login events recorded with different timezone offsets.
Each event is a string "name YYYY-MM-DDTHH:MM+HH:MM" or "name YYYY-MM-DDTHH:MM-HH:MM": the learner's name, the local date and time of the login, and the offset of that local time from UTC (so 2024-03-01T20:00-05:00 happened at 01:00 UTC on March 2).
The board uses a single reporting timezone tz (a UTC offset such as "+09:00"). A learner is active on a calendar day if at least one of their logins falls on that day *as seen in the reporting timezone* (convert the instant to tz; do not use the date written in the event). Several logins on one day count as one active day. Days are real calendar days, so February has 29 days in 2024 and year ends roll over normally.
A streak is a maximal run of consecutive active days. For each learner report:
longest: the length of their longest streak;current: the length of the streak that ends on their most recent active day, but only if that day istodayor the day before it (the learner can still extend it today); otherwise0.
today is a date, "YYYY-MM-DD", in the reporting timezone. Events on days after today are ignored as if they were not in the log, and a learner with no remaining events is not reported. Names are case-sensitive.
Input: events, a list of strings in the format above; today, a string; tz, a string "+HH:MM" or "-HH:MM".
Output: a list of strings "name longest current", one per reported learner, ordered by longest descending, then current descending, then name ascending lexicographically.
Example 1
- Input:
- [["ana 2024-03-01T09:00+00:00","ana 2024-03-02T08:00+00:00","ana 2024-03-02T22:00+00:00","ana 2024-03-03T10:00+00:00","ana 2024-03-05T10:00+00:00","bo 2024-02-27T12:00+00:00","bo 2024-02-28T12:00+00:00","bo 2024-02-29T12:00+00:00","bo 2024-03-01T12:00+00:00","bo 2024-03-02T12:00+00:00","bo 2024-03-03T12:00+00:00","cy 2024-03-04T23:59+00:00","cy 2024-03-06T00:00+00:00"],"2024-03-05","+00:00"]
- Output:
- ["bo 6 0","ana 3 1","cy 1 1"]
- Explanation
- With
tz+00:00 and today March 5, 2024: ana is active on March 1, 2, 3 and 5 (the two logins on March 2 count once): the longest streak is 3, but the streak ending on her last active day (March 5) is just that one day. bo is active every day from February 27 to March 3, crossing the leap day February 29: 6 days, but her last day is two days before today, so the current streak is 0. cy's login on March 4 is yesterday, giving 1 and 1; his login on March 6 is in the future and is ignored.
Example 2
- Input:
- [["dee 2024-03-01T20:00-05:00","dee 2024-03-02T23:30+00:00","dee 2024-03-03T16:00+00:00","eli 2024-03-04T00:30+09:00","eli 2024-03-02T15:30+00:00","eli 2024-03-01T00:30+09:00"],"2024-03-04","+09:00"]
- Output:
- ["dee 3 3","eli 2 2"]
- Explanation
- With
tz+09:00, dee's logins are converted:2024-03-01T20:00-05:00is 10:00 on March 2 in +09:00,2024-03-02T23:30+00:00is 08:30 on March 3, and2024-03-03T16:00+00:00is 01:00 on March 4. That makes three consecutive days even though the written dates are March 1, 2 and 3. Today is March 4, so her current streak is 3. eli logged in on March 4 and, converted, on March 3 (2024-03-02T15:30+00:00) and once earlier on March 1 (2024-03-01T00:30+09:00): the longest streak is 2 and the current one is 2.
Example 3
- Input:
- [["fay 2024-12-31T09:00-08:00","fay 2025-01-01T09:00-08:00","fay 2025-01-02T09:00-08:00","gus 2025-01-02T07:30+00:00","hal 2025-01-03T09:00-08:00","hal 2025-01-04T09:00-08:00"],"2025-01-02","-08:00"]
- Output:
- ["fay 3 3","gus 1 1"]
- Explanation
- With
tz-08:00 and today January 2, 2025: fay is active on December 31, January 1 and January 2, so 3 and 3 across the year boundary. gus's single login,2025-01-02T07:30+00:00, is 23:30 on January 1 in -08:00, so it is yesterday: 1 and 1. hal only has logins after today, so is not reported.
Constraints
0 <= events.length <= 2,000- Dates are valid calendar dates between 2000-01-01 and 2099-12-31; offsets are between
-12:00and+14:00 - Names are 1 to 10 ASCII letters or digits;
todayis a valid date - The reporting timezone is also a valid offset in the same range