Quick Overview

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.

Find minimal time to serve customers

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You manage a bank with k tellers; teller i takes serviceTimes[i] minutes to serve one customer and works continuously. Given an integer array serviceTimes and an integer m (customers to serve), return the minimum integer time T such that sum over i of floor(T / serviceTimes[i]) >= m. Describe your algorithm, prove correctness, analyze time/space complexity, and implement it. Discuss edge cases (very large m, slow tellers, identical rates).

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.

You manage a bank with `k` tellers. Teller `i` takes `serviceTimes[i]` minutes to serve one customer and works continuously without breaks. Given the integer array `serviceTimes` and an integer `m` (the number of customers that must be served), return the minimum integer time `T` such that the total number of customers served by all tellers within `T` minutes is at least `m`. Formally, return the smallest integer `T` with `sum over i of floor(T / serviceTimes[i]) >= m`. Within `T` minutes, teller `i` can serve `floor(T / serviceTimes[i])` customers. As `T` increases the total served is non-decreasing, so the predicate `served(T) >= m` is monotone and the answer can be found with binary search on `T`. A safe upper bound is `min(serviceTimes) * m`, because the single fastest teller alone serves `m` customers by that time. Example: `serviceTimes = [1, 2, 3]`, `m = 5`. At `T = 3` the tellers serve `3 + 1 + 1 = 5 >= 5`, while at `T = 2` they serve `2 + 1 + 0 = 3 < 5`, so the answer is `3`.

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.

Hints

  1. 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.
  2. 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.
  3. 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.
  4. Watch the integer range: with large m, min(serviceTimes) * m can overflow 32-bit integers. Use long in Java and long long in C++.

Loading coding console...