PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to process timestamped event logs and extract ordered length-3 visit sequences, testing skills in sorting, frequency aggregation, set-based deduplication, combinatorial pattern enumeration, and efficient use of data structures.

  • hard
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Find the Most Common Visit Pattern

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

You are given a list of web visit logs. Each log record contains userId, timestamp, and page. For each user, sort that user's visits by timestamp. A length-3 visit pattern is an ordered sequence of three pages visited by the same user in chronological order, not necessarily consecutively. Find the length-3 page pattern visited by the largest number of distinct users. A user contributes at most once to the count of a given pattern, even if the user has the same pattern multiple times. If multiple patterns have the same highest count, return the lexicographically smallest pattern. Discuss complexity and edge cases, including users with fewer than three visits.

Quick Answer: This question evaluates the ability to process timestamped event logs and extract ordered length-3 visit sequences, testing skills in sorting, frequency aggregation, set-based deduplication, combinatorial pattern enumeration, and efficient use of data structures.

You are given three parallel arrays describing website visit logs: userId, timestamp, and page. Each log entry represents one visit made by userId[i] to page[i] at time timestamp[i]. For each user, first sort that user's visits by timestamp. A length-3 visit pattern is an ordered sequence of three pages visited by the same user in chronological order, and the three visits do not need to be consecutive. Your task is to find the length-3 page pattern visited by the largest number of distinct users. A user contributes at most once to a pattern's count, even if that user can form the same pattern multiple times. If multiple patterns have the same highest count, return the lexicographically smallest pattern. Ignore users with fewer than three visits. If no valid length-3 pattern exists, return an empty list.

Constraints

  • 0 <= n <= 200, where n is the number of log records
  • len(userId) == len(timestamp) == len(page) == n
  • All timestamps are distinct integers
  • 1 <= len(userId[i]), len(page[i]) <= 20 for valid indices i

Examples

Input: (["joe","joe","joe","james","james","james","james","mary","mary","mary"], [1,2,3,4,5,6,7,8,9,10], ["home","about","career","home","cart","maps","home","home","about","career"])

Expected Output: ["home", "about", "career"]

Explanation: Joe and Mary both visited the pattern ["home", "about", "career"]. James has different 3-page patterns, so this pattern has the highest distinct-user count of 2.

Input: (["u1","u1","u1","u2","u2","u2"], [1,2,3,4,5,6], ["a","b","c","a","c","b"])

Expected Output: ["a", "b", "c"]

Explanation: User u1 contributes pattern ["a", "b", "c"] and user u2 contributes ["a", "c", "b"]. Both have count 1, so the lexicographically smaller pattern ["a", "b", "c"] is returned.

Input: (["u1","u1","u1","u1","u1","u2","u2","u2"], [5,1,3,2,4,8,6,7], ["a","a","a","b","b","a","a","b"])

Expected Output: ["a", "b", "a"]

Explanation: After sorting by timestamp, u1 visited ["a", "b", "a", "b", "a"] and u2 visited ["a", "b", "a"]. The pattern ["a", "b", "a"] appears multiple times for u1 but counts only once for that user, so its distinct-user count is 2.

Input: (["u1","u1","u2"], [1,2,3], ["x","y","z"])

Expected Output: []

Explanation: u1 has only 2 visits and u2 has only 1 visit, so no user can form a length-3 pattern.

Input: ([], [], [])

Expected Output: []

Explanation: There are no logs, so no valid 3-page pattern exists.

Hints

  1. Sort the visits by timestamp first, then collect each user's pages in chronological order.
  2. For each user, generate all unique 3-page combinations with a set so the same user is counted at most once per pattern.
Last updated: May 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Datacenter Router Commands - Amazon (hard)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)
  • Maximize Value Under a Budget - Amazon (medium)
  • Merge Multiple Sorted Arrays - Amazon (medium)