Find Shortest Queue with Few Calls
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates algorithm design and analysis skills, focusing on working with restricted data structure APIs and minimizing total operations through API call optimization while processing queues.
Constraints
- 0 <= number of queues <= 10^5
- 0 <= total number of integers across all queues <= 2 * 10^5
- -10^9 <= each queue element <= 10^9
Examples
Input: [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
Expected Output: (2, 9)
Explanation: The shortest queue is [4, 5], so the minimum length is 2 and the sum is 4 + 5 = 9.
Input: [[], [1, 2, 3], [4]]
Expected Output: (0, 0)
Explanation: An empty queue already exists, so the minimum length is 0 and the sum of that queue is 0.
Input: [[-1, -2], [5], [7, 8]]
Expected Output: (1, 5)
Explanation: The queue [5] is the shortest, with length 1 and sum 5.
Input: [[2, 2], [1, 1], [3, 3, 3]]
Expected Output: (2, 4)
Explanation: Two queues have minimum length 2. The reference solution returns the first shortest queue in input order, [2, 2], whose sum is 4.
Input: []
Expected Output: (0, 0)
Explanation: With no queues at all, the defined result is (0, 0).
Input: [[10, -3, 5]]
Expected Output: (3, 12)
Explanation: There is only one queue, so its length is 3 and its sum is 10 + (-3) + 5 = 12.
Hints
- Instead of finishing one queue before moving to the next, think about processing all queues one level at a time.
- At the start of round k, if one queue is already empty, then every queue that survived the previous k rounds must have length at least k.