As reported: Amazon loop (APAC, Prime Video interviewers, Sept 2025), round 2 with an SDE-2: given
Nsorted vectors, find all elements whose frequency is at leastK. A brute force with hash maps was discussed first, a binary-search idea was rejected, and the optimal solution merged the vectors with a priority queue while counting.
You are given arrays, a list of N arrays, each sorted in non-decreasing order, and an integer k. Return, in increasing order, every distinct value whose total number of occurrences across all arrays is at least k.
Example 1
arrays = [[1,2,2,5],[2,3,5],[1,5,9]], k = 3
Output: [2,5] 2 occurs 3 times, 5 occurs 3 times, 1 only twice
Example 2
arrays = [[4,4,4],[1]], k = 2
Output: [4]
Constraints: 1 ≤ N ≤ 10⁴; each array has 0 to 10⁵ elements, at most 10⁵ elements in total; −10⁹ ≤ value ≤ 10⁹; 1 ≤ k ≤ 10⁵.
Code runner format: two lines: arrays as a JSON array of arrays, then k. Implement frequentElements(arrays, k); the harness prints the list.
Clarifications
- Occurrences inside one array count separately:
[4,4,4]contributes three 4s. - Return an empty list when no value qualifies.