Find the Most Frequent Request IDs
AmazonSoftware Engineer · OnsiteLast updated
Implement an algorithm to identify the top k most frequent request IDs from a stream of log events, breaking frequency ties by smaller ID values and handling cases where k exceeds the number of unique IDs.
Problem Statement
You are monitoring service traffic on an internal microservice at Amazon. Given an array of integers request_ids representing incoming requests in a log window, and an integer k, return the k most frequent request IDs.
The results should be sorted primarily by frequency in descending order. If two request IDs have the same frequency, break the tie by choosing the smaller request ID first (in ascending numerical order).
If k is greater than or equal to the total number of distinct request IDs present in request_ids, return all distinct request IDs ordered according to the ranking rules above.
Function Interface
def top_k_request_ids(request_ids: list[int], k: int) -> list[int]:
pass
Constraints
0 <= len(request_ids) <= 10^51 <= request_ids[i] <= 10^91 <= k <= 10^5
Examples
Example 1
Input: request_ids = [101, 202, 101, 303, 202, 101, 404], k = 2
Output: [101, 202]
Explanation:
- 101 appears 3 times.
- 202 appears 2 times.
- 303 appears 1 time.
- 404 appears 1 time.
The top 2 most frequent IDs are 101 and 202.
Example 2
Input: request_ids = [500, 300, 500, 300, 200], k = 5
Output: [300, 500, 200]
Explanation:
- 300 appears 2 times.
- 500 appears 2 times.
- 200 appears 1 time.
Both 300 and 500 have a frequency of 2. By tie-breaker rule, 300 precedes 500. There are only 3 distinct IDs, so all 3 are returned.
Edge Cases
- Empty input list: should return
[]. - All IDs have the same frequency: output sorted in ascending order of IDs.
kis larger than the number of unique IDs: return all unique IDs sorted by the frequency and tie-breaking rules.
Hints
Hint 1
Start by aggregating counts of each request ID into a hash map or frequency dictionary.
Hint 2
Consider sorting the unique IDs using a tuple `(-frequency, request_id)` to handle both the frequency ranking and the tie-breaker simultaneously.
Hint 3
For large inputs where k is significantly smaller than the number of unique IDs, a min-heap or Quickselect algorithm can optimize the runtime beyond full sorting.
Follow-up questions
- How would you adapt your approach if request IDs arrive as an infinite stream and you must answer top-k queries in real time?
- Could you achieve O(N) average time complexity using bucket sort or Quickselect instead of O(U log U) where U is the number of unique IDs?
- How would this architecture scale if the log events were distributed across dozens of worker nodes?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.