Find minimal time to serve customers
Company: Pinterest
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: Find minimal time to serve customers evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= k = serviceTimes.length
- 1 <= serviceTimes[i] (each teller's per-customer time is a positive integer)
- 1 <= m (at least one customer must be served)
- The answer T can be large; use 64-bit integers in Java/C++ (min(serviceTimes) * m may exceed 32-bit range for very large m).
Examples
Input: ([1, 2, 3], 5)
Expected Output: 3
Explanation: At T=3: 3//1 + 3//2 + 3//3 = 3+1+1 = 5 >= 5. At T=2 it is only 3, so 3 is minimal.
Input: ([2], 1)
Expected Output: 2
Explanation: Single teller needs 2 minutes to serve the one required customer.
Input: ([5, 10, 10], 9)
Expected Output: 25
Explanation: At T=25: 25//5 + 25//10 + 25//10 = 5+2+2 = 9 >= 9. At T=24 it is 4+2+2 = 8 < 9.
Input: ([1, 1, 1], 6)
Expected Output: 2
Explanation: Identical rates: at T=2 each serves 2, total 6 >= 6; at T=1 total is 3 < 6.
Input: ([7], 1000000)
Expected Output: 7000000
Explanation: Large-m stress: one teller at 7 min/customer needs 7*1000000 minutes for 1,000,000 customers.
Input: ([3, 3, 3], 1)
Expected Output: 3
Explanation: Boundary m=1: any teller serves its first customer at T=3, so the minimum is 3.
Hints
- The number of customers served by time T, sum(floor(T / serviceTimes[i])), only grows as T grows. This monotonicity is exactly what binary search needs.
- Binary search on the answer T rather than on an index. The predicate is `served(T) >= m`; find the smallest T where it becomes true.
- A correct upper bound is min(serviceTimes) * m: by that time the single fastest teller alone has already served m customers, so the predicate is guaranteed true.
- Watch the integer range: with large m, min(serviceTimes) * m can overflow 32-bit integers. Use long in Java and long long in C++.