You are given three arrays of strings username
, timestamp
and website
of the same length n
, where username[i]
, timestamp[i]
and website[i]
indicate that user username[i]
visited the website website[i]
at time timestamp[i]
.
A pattern is a list of three websites (not necessarily distinct) visited by one user.
["home", "away", "love"]
, ["leetcode", "love", "leetcode"]
, and ["luffy", "luffy", "luffy"]
are all patterns.The score of a pattern is the number of users that visited in order all the websites in the pattern.
["home", "away", "love"]
, and users ["alice","bob","alice","eve"]
visited the websites ["home","away","away","home"]
, ["away","love","love","away"]
, ["home","away","love","love"]
, then the score of the pattern is 2
because both "alice"
and "eve"
visited in order the three websites in the pattern.You are asked to find the most frequent pattern. If there is more than one most frequent pattern, return the lexicographically smallest one.
Return the most frequent pattern.
Example 1:
Input: username = ["joe","joe","joe","james","james","james","james","mary","mary","mary"], timestamp = [1,2,3,4,5,6,7,8,9,10], website = ["home","about","career","home","cart","maps","home","home","about","career"] Output: ["home","about","career"] Explanation: The pattern ["home","about","career"] has score 2 (joe and mary). The pattern ["home","cart","maps"] has score 1 (james). The pattern ["home","cart","home"] has score 1 (james). The pattern ["home","maps","home"] has score 1 (james). The pattern ["cart","maps","home"] has score 1 (james). So the pattern ["home","about","career"] is the most frequent pattern, so you should return it.
Example 2:
Input: username = ["uaice","uaice","uaice","alice","alice","alice","alice","bob","bob","bob"], timestamp = [1,2,3,4,5,6,7,8,9,10], website = ["a","b","c","a","b","c","d","a","b","c"] Output: ["a","b","c"]
Constraints:
n == username.length == timestamp.length == website.length
3 <= n <= 50
1 <= username[i].length <= 10
0 <= timestamp[i] <= 109
1 <= website[i].length <= 10
username[i]
and website[i]
consist of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the most frequent user visit pattern on a website, the brute force approach considers every possible sequence of three website visits a user makes. We systematically examine all combinations of three websites visited by each user to see which combination occurs most often across all users.
Here's how the algorithm would work step-by-step:
def analyze_user_website_visit_pattern(username, timestamp, website):
user_website_visits = {}
for i in range(len(username)):
user = username[i]
time = timestamp[i]
site = website[i]
if user not in user_website_visits:
user_website_visits[user] = []
user_website_visits[user].append((time, site))
for user in user_website_visits:
user_website_visits[user].sort()
all_sequences = {}
for user, visits in user_website_visits.items():
websites = [visit[1] for visit in visits]
sequences = set()
for i in range(len(websites)):
for j in range(i + 1, len(websites)):
for k in range(j + 1, len(websites)):
sequences.add((websites[i], websites[j], websites[k]))
# Count the frequency of each sequence
for sequence in sequences:
if sequence not in all_sequences:
all_sequences[sequence] = 0
all_sequences[sequence] += 1
most_frequent_sequence = None
max_frequency = 0
# Find the sequence with the highest frequency
for sequence, frequency in all_sequences.items():
if frequency > max_frequency:
max_frequency = frequency
most_frequent_sequence = sequence
elif frequency == max_frequency:
if sequence < most_frequent_sequence:
most_frequent_sequence = sequence
return list(most_frequent_sequence)
The goal is to find the most frequent sequence of three website pages visited by each user. The clever approach involves organizing visits chronologically and then efficiently identifying all possible three-page sequences for each person.
Here's how the algorithm would work step-by-step:
def analyze_user_website_visit_pattern(username, timestamp, website):
user_visits = {}
for i in range(len(username)):
user = username[i]
time = timestamp[i]
webpage = website[i]
if user not in user_visits:
user_visits[user] = []
user_visits[user].append((time, webpage))
three_sequence_counts = {}
for user, visits in user_visits.items():
# Sort visits chronologically for each user.
visits.sort()
webpages = [visit[1] for visit in visits]
sequences = set()
# Generate all possible three-page sequences.
for i in range(len(webpages) - 2):
sequence = (webpages[i], webpages[i+1], webpages[i+2])
sequences.add(sequence)
for sequence in sequences:
if sequence not in three_sequence_counts:
three_sequence_counts[sequence] = 0
three_sequence_counts[sequence] += 1
most_frequent_sequence = None
max_count = 0
#Find the sequence with the highest frequency,
#or the smallest sequence if there are ties.
for sequence, count in three_sequence_counts.items():
if count > max_count:
max_count = count
most_frequent_sequence = sequence
elif count == max_count:
if sequence < most_frequent_sequence:
most_frequent_sequence = sequence
return list(most_frequent_sequence)
Case | How to Handle |
---|---|
Empty username, timestamp, or website arrays | Return an empty list immediately as no patterns can be formed. |
Arrays of different lengths | Throw an IllegalArgumentException or return an empty list if array lengths differ. |
Arrays with fewer than 3 visits by any single user | Ignore users with fewer than 3 visits when generating patterns. |
Large input arrays (performance concerns) | Ensure the solution uses efficient data structures (e.g., HashMaps) and algorithms to handle large inputs without exceeding time limits. |
Multiple patterns with the same highest similarity score | Store all patterns with the highest score and return the lexicographically smallest one. |
Timestamps not in ascending order for a given user | Sort the visits by timestamp for each user before generating patterns. |
Users visiting the same website multiple times in a row | Ensure the pattern generation logic considers consecutive visits to the same website as part of valid 3-sequence patterns. |
All users have the same visit patterns | The solution correctly identifies and returns the most frequent pattern even when all users share it. |