Most Frequent Number Following Key In an Array - Problem

Imagine you're analyzing a sequence of events where you want to find patterns of what happens immediately after a specific trigger event occurs.

You are given a 0-indexed integer array nums and an integer key that is guaranteed to be present in the array. Your task is to find which number appears most frequently immediately after the key.

Specifically:

  • For every occurrence of key at index i (where i < nums.length - 1)
  • Look at the next element nums[i + 1]
  • Count how many times each unique number appears in these "next" positions
  • Return the number with the highest count

Example: If nums = [1, 100, 200, 1, 100] and key = 1, then after the first occurrence of 1, we see 100, and after the second occurrence of 1, we also see 100. So 100 appears 2 times after key, making it our answer.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,100,200,1,100], key = 1
โ€บ Output: 100
๐Ÿ’ก Note: The key 1 appears at indices 0 and 3. After index 0, the next number is 100. After index 3, the next number is also 100. So 100 appears 2 times after key, making it the most frequent.
example_2.py โ€” Multiple Targets
$ Input: nums = [2,2,2,2,3], key = 2
โ€บ Output: 2
๐Ÿ’ก Note: The key 2 appears at indices 0, 1, 2, and 3. The next numbers are 2, 2, 2, and 3 respectively. Number 2 appears 3 times after key, while 3 appears only once. So 2 is the most frequent.
example_3.py โ€” Single Occurrence
$ Input: nums = [1,1000,2], key = 1
โ€บ Output: 1000
๐Ÿ’ก Note: The key 1 appears only at index 0, and the next number is 1000. Since 1000 is the only number following key, it's the most frequent (and only) target.

Constraints

  • 2 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 1000
  • key is guaranteed to be present in nums
  • The test cases guarantee that the target with maximum count is unique

Visualization

Tap to expand
Customer Purchase Pattern AnalysisPurchase Sequence: [Coffee, Donut, Tea, Coffee, Donut]Key Product: Coffeeโ˜•Coffee๐ŸฉDonut๐ŸตTeaโ˜•Coffee๐ŸฉDonutAnalysis Results:Next Purchase CounterCoffee โ†’ Donut: โœ“โœ“ (Count: 2)Most Frequent: Donut๐Ÿ“Š Customers who buy Coffee typically buy Donut nextAlgorithm Insight:1. Scan sequence left to right2. When key found โ†’ count next item3. Track maximum count efficientlyโšก Single pass O(n) solution!
Understanding the Visualization
1
Scan Purchase History
Go through all purchases in chronological order
2
Identify Key Purchases
When you see the key product being bought, look at the very next purchase
3
Count Next Purchases
Keep a tally of what products are bought immediately after the key product
4
Find Most Popular
The product with the highest count is the most frequent 'next purchase'
Key Takeaway
๐ŸŽฏ Key Insight: Pattern detection in sequential data can be solved efficiently by tracking frequencies during a single scan, making it perfect for real-time analysis of customer behavior, user interactions, or any sequential event data.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
38.2K Views
Medium Frequency
~12 min Avg. Time
890 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