Analyze User Website Visit Pattern - Problem

You are given three arrays username, website, and timestamp, all of the same length. The tuple [username[i], website[i], timestamp[i]] indicates that user username[i] visited website website[i] at time timestamp[i].

A pattern is a list of three websites (not necessarily distinct). The score of a pattern is the number of users that visited all websites in the pattern in the same order they appeared in the pattern (chronologically).

Return the pattern with the largest score. If there are multiple patterns with the same largest score, return the lexicographically smallest pattern.

Input & Output

Example 1 — Multiple Users Same Pattern
$ Input: username = ["joe","joe","joe","james","james","james"], website = ["home","about","career","home","about","career"], timestamp = [1,2,3,4,5,6]
Output: ["home","about","career"]
💡 Note: Both users 'joe' and 'james' visited home→about→career in chronological order, giving this pattern a score of 2, which is the maximum possible.
Example 2 — Lexicographic Tie Breaking
$ Input: username = ["ua","ua","ua","ub","ub","ub"], website = ["a","b","a","a","b","c"], timestamp = [1,2,3,4,5,6]
Output: ["a","b","a"]
💡 Note: Two patterns have score 1: ["a","b","a"] and ["a","b","c"]. We return the lexicographically smaller one.
Example 3 — Same User Multiple Patterns
$ Input: username = ["u1","u1","u1","u1"], website = ["a","b","c","d"], timestamp = [1,2,3,4]
Output: ["a","b","c"]
💡 Note: User 'u1' generates multiple 3-website patterns: ["a","b","c"], ["a","b","d"], ["a","c","d"], ["b","c","d"]. All have score 1, so we return the lexicographically smallest.

Constraints

  • 3 ≤ username.length ≤ 50
  • 1 ≤ username[i].length ≤ 10
  • 1 ≤ website[i].length ≤ 10
  • 1 ≤ timestamp[i] ≤ 109
  • All tuples [username[i], website[i], timestamp[i]] are unique

Visualization

Tap to expand
Analyze User Website Visit Pattern INPUT User Site Time joe home 1 joe about 2 joe career 3 james home 4 james about 5 james career 6 Grouped by User: Joe home about career James home about career Sorted by timestamp time --> ALGORITHM STEPS 1 Group by User Sort visits by timestamp for each user 2 Generate 3-Patterns For each user, generate all 3-website combos Joe's patterns: (home,about,career) James's patterns: (home,about,career) 3 Count Pattern Scores Count unique users per pattern (not visits) Pattern Score (home,about,career) 2 4 Select Best Pattern Max score, then lex order FINAL RESULT Winning Pattern: home about career --> --> Score: 2 users Joe James OUTPUT: ["home","about","career"] OK - Verified Both users visited this exact sequence Key Insight: The pattern score counts UNIQUE USERS, not total visits. Each user contributes at most 1 to a pattern's score, even if they visit the same sequence multiple times. Generate all C(n,3) combinations per user using sorted timestamps, then count distinct users per pattern. Use lexicographic comparison to break ties. TutorialsPoint - Analyze User Website Visit Pattern | Optimized Pattern Generation
Asked in
Amazon 15 Google 12 Facebook 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen