Group Timestamps into Consecutive-Day Intervals
Updated
Problem statement
Given a list timestamps of times written as yyyy-mm-dd-hh-mm, in no particular order, return the intervals of consecutive active days. A day is active if at least one timestamp falls on it. Only the date part yyyy-mm-dd decides the day, and the hour and minute do not matter.
An interval is a longest run of consecutive calendar days that are all active. Write it as [startDate, endDate], with both dates in the form yyyy-mm-dd. A run of one day is [d, d]. Every active day belongs to exactly one interval.
Return the intervals in the order their first timestamp appears in timestamps: the interval holding the day of timestamps[0] comes first, then the interval of the first timestamp that is not in an interval listed so far, and so on.
Try to solve it in O(n) time without sorting, where n is the length of timestamps.
Note: days follow the Gregorian calendar. A year is a leap year when it is divisible by 4, except years divisible by 100 but not by 400. February has 29 days in a leap year and 28 days otherwise.
Function
activeDayIntervals(timestamps: List<String>) → List<List<String>>Examples
Example 1
timestamps = ["2026-03-02-09-15","2026-02-27-23-59","2026-03-01-00-05","2026-02-28-12-00","2026-03-05-08-30"]return = [["2026-02-27","2026-03-02"],["2026-03-05","2026-03-05"]]2026 is not a leap year, so March 1 comes right after February 28. The days February 27, February 28, March 1 and March 2 form one interval. March 5 stands alone. The first timestamp falls on March 2, so its interval comes first.
Example 2
timestamps = ["2024-02-29-10-00","2024-03-01-07-45","2023-12-31-22-10","2024-01-01-00-00","2024-02-29-18-30","2024-02-27-06-00"]return = [["2024-02-29","2024-03-01"],["2023-12-31","2024-01-01"],["2024-02-27","2024-02-27"]]2024 is a leap year, so February 29 and March 1 are one interval, and February 27 is cut off from it because February 28 has no timestamp. December 31, 2023 and January 1, 2024 are also consecutive. The intervals are listed in the order their first timestamp appears: index 0, then 2, then 5.
Example 3
timestamps = ["2100-03-01-00-00","2100-02-28-23-59"]return = [["2100-02-28","2100-03-01"]]2100 is divisible by 100 but not by 400, so it is not a leap year and March 1 follows February 28. Both days form one interval.
Constraints
1 <= timestamps.length <= 10^4- Each
timestamps[i]has the formyyyy-mm-dd-hh-mmand is a real date and time. 1900 <= yyyy <= 2100,00 <= hh <= 23and00 <= mm <= 59.- Timestamps may repeat, and many may fall on the same day.
More Duolingo Phone Screen questions
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
- Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
- Sum All Values in a Binary TreePHONE SCREEN · Seen Oct 2026
- User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
- Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026
- Validate a Mahjong Hand PartitionPHONE SCREEN · Seen Nov 2025
- Directional Tiles: Find a Valid Reconfiguration PathPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Shipping Cost Calculator Part 3 - Mixed Fixed/Incremental TiersStripe · PHONE SCREENStripe interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Classify Password StrengthHackerRank · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN
- Group AnagramsWorkday · PHONE SCREEN
- Video Cache Hit Rates from a CSV LogTubi · PHONE SCREEN
- Longest Substring Without Repeating CharactersNykaa · PHONE SCREENNykaa interview questions
- Apply Operations to Make a String EmptyFlexport · PHONE SCREENFlexport interview questions