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
Website Visit Pattern AnalysisRaw Data:Username[joe,joe,james...]Website[home,about,home...]Timestamp[1,2,4...]Grouped by User:joe (sorted by time)home(1) → about(2) → career(3)→ home(4)james (sorted by time)home(4) → about(5) → career(6)Pattern Counting:[home,about,career]Score: 2 users[home,about,home]Score: 1 userResult: ["home","about","career"]Pattern with maximum score
Understanding the Visualization
1
Input Data
Three parallel arrays: usernames, websites, timestamps
2
Group & Sort
Organize visits by user and sort chronologically
3
Generate Patterns
Create all possible 3-website subsequences
4
Count & Select
Find pattern with highest user count
Key Takeaway
🎯 Key Insight: Group visits by user first, then systematically generate all 3-website patterns while preserving chronological order
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