FastPrepMost Frequent Consecutive Website Pattern

Most Frequent Consecutive Website Pattern

Amazon logoAmazon● MediumNEW GRADONSITE INTERVIEW

Problem statement

You are given three equal-length arrays describing website visits. Entry i contains a username, an integer timestamp, and a website.

For each user, sort visits by timestamp; when timestamps are equal, keep their original input order. Every three adjacent visits in that per-user order form a consecutive website pattern. A user contributes at most once to the count of any distinct pattern, even if that pattern occurs several times for the user.

Return the three websites in the pattern seen by the greatest number of distinct users. If several patterns have the same count, return the lexicographically smallest three-element sequence. If no user has at least three visits, return an empty array.

Function

mostFrequentConsecutivePattern(usernames: String[], timestamps: int[], websites: String[]) → String[]

Examples

Example 1

usernames = ["amy","amy","amy","ben","ben","ben"]timestamps = [1,2,3,1,2,3]websites = ["home","cart","pay","home","cart","pay"]return = ["home","cart","pay"]

Both users have the consecutive pattern [home, cart, pay], so it has two distinct-user supporters.

Example 2

usernames = ["a","a","a","b","b","b"]timestamps = [3,1,2,1,2,3]websites = ["z","a","z","a","z","z"]return = ["a","z","z"]

User a produces [a, z, z], and user b produces the same pattern. Sorting is performed within each user rather than on the global input.

Example 3

usernames = ["a","a","b"]timestamps = [1,2,1]websites = ["x","y","z"]return = []

No user has three visits, so no consecutive triple exists.

Constraints

  • 0 <= usernames.length <= 5000.
  • usernames.length == timestamps.length == websites.length.
  • Usernames and websites contain 1 to 30 lowercase English letters.
  • 0 <= timestamps[i] <= 10^9.
  • Input rows may be globally unordered; equal timestamps for one user retain input order.

More Amazon problems

See Amazon hiring insights
Saved
public String[] mostFrequentConsecutivePattern(String[] usernames, int[] timestamps, String[] websites) {
    // Write your solution here.
}
usernames["amy","amy","amy","ben","ben","ben"]
timestamps[1,2,3,1,2,3]
websites["home","cart","pay","home","cart","pay"]
expected["home", "cart", "pay"]
Checking account…