KatInterview

Identify Most Popular Three-Page User Sequence

Arrays & HashingAuto-graded · 13 test cases
HardAuto-gradedArrays & Hashingarrayshash-tablesorting

Last updated

Process website activity logs to compute chronological 3-site visit subsequences and find the most widely performed pattern across unique users.

You are given website analytics data consisting of three parallel arrays: username, timestamp, and website. Each entry represents a visit to a specific page by a user at a given time point.

A 3-page visit sequence for a user is any ordered triple of web pages $(S_1, S_2, S_3)$ formed by three of that user's visits selected at indices $i < j < k$ after ordering all of that user's visits chronologically by timestamp. Note that the visits do not need to occur consecutively in time.

If a single user produces the exact same 3-page sequence through multiple different index combinations, that sequence is counted only once for that user.

Your objective is to find the 3-page sequence that is observed across the maximum number of distinct users. If there is a tie between multiple 3-page sequences with the highest user count, return the sequence that is lexicographically smallest.

If no user has at least 3 page visits, return an empty list [].

Format

The output should be a list of three strings representing the sequence [S1, S2, S3].

Constraints

  • $n = \text{len(username)} = \text{len(timestamp)} = \text{len(website)}$
  • $1 \le n \le 10,000$
  • $1 \le \text{len(username[i])}, \text{len(website[i])} \le 10$
  • All timestamps for a given user are unique.

Examples

Example 1

Input: find_most_common_pattern(['joe', 'joe', 'joe', 'james', 'james', 'james'], [1, 2, 3, 4, 5, 6], ['home', 'about', 'career', 'home', 'cart', 'books'])

Output: ['home', 'about', 'career']

Hints

Hint 1

Group page visits by username and sort each user's history by timestamp.

Hint 2

Generate all unique 3-element subsequences for each user using a set so that each sequence is counted at most once per user.

Hint 3

Use a hash map to aggregate how many unique users performed each 3-element sequence, then select the max-count sequence (using lexicographical order as a tie-breaker).