Most Frequent Three-Page Sequences
Updated
Problem statement
A website keeps a log of page visits in the order they happened. Each entry logs[i] = [user, page] says that user opened page, and an entry with a smaller index happened earlier.
Read each user's own visits in log order. A three-page sequence is three pages that one user opened one right after another: three back-to-back entries in that user's own list of visits. Entries of other users in between do not break a sequence. A user with v visits gives v - 2 sequences, or none when v is less than 3. A sequence counts once every time it appears, even when the same user repeats it.
Return the k sequences that appear most often across all users, each written as [page1, page2, page3]. List them from most to fewest appearances. When two sequences appear the same number of times, the one whose first page comes first in alphabetical order goes first; if the first pages match, compare the second pages, then the third. If there are fewer than k different sequences, return all of them.
Function
topPageSequences(logs: List<List<String>>, k: int) → List<List<String>>Examples
Example 1
logs = [["ann","home"],["bob","home"],["cat","search"],["ann","search"],["bob","search"],["ann","listing"],["cat","listing"],["bob","listing"],["ann","home"]]k = 1return = [["home","search","listing"]]In log order, ann opens home, search, listing, home; bob opens home, search, listing; cat opens search, listing. ann gives two sequences, [home, search, listing] and [search, listing, home], and bob gives [home, search, listing]. cat has only 2 visits, so cat gives none. [home, search, listing] appears 2 times and [search, listing, home] 1 time, so with k = 1 the answer holds the first one.
Example 2
logs = [["dee","home"],["dee","map"],["dee","saved"],["dee","home"],["dee","map"],["dee","saved"]]k = 2return = [["home","map","saved"],["map","saved","home"]]dee gives four sequences: [home, map, saved], [map, saved, home], [saved, home, map] and [home, map, saved] again, so the first one appears 2 times. The other two appear 1 time each. They tie, and map comes before saved in alphabetical order, so [map, saved, home] takes the second place.
Example 3
logs = [["eve","home"],["fay","search"],["eve","home"],["fay","home"],["eve","home"]]k = 10return = [["home","home","home"]]eve opens home 3 times, which gives the one sequence [home, home, home]; a page may repeat inside a sequence. fay has only 2 visits. There is 1 sequence in total, fewer than k = 10, so the answer holds just that one.
Constraints
1 <= logs.length <= 5000logs[i].length == 2- Each
userand eachpagehas 1 to 10 lowercase English letters. 1 <= k <= 10
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Group AnagramsWorkday · PHONE SCREEN
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEWGoogle interview questions
- Find Duplicate File GroupsAnthropic · PHONE SCREENAnthropic interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Group Duplicate Files by ContentPostman · ONSITE INTERVIEWPostman interview questions