Longest run of customers a bank can serve
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Google. It tests sliding window, two pointers and prefix sums.
A bank opens with initial dollars in its vault (initial >= 0). Customers wait in a fixed queue, given as an integer array transactions: a positive value is a deposit and a negative value is a withdrawal of that size.
The bank may turn away any number of customers at the front of the queue and pick one customer to start with. From then on it must serve customers strictly in queue order. It stops as soon as the next customer's withdrawal would make the vault balance negative (a balance of exactly 0 is fine). That customer and everyone after them go unserved.
Implement:
def max_served(initial: int, transactions: list[int]) -> tuple[int, int, int]:
...
Return (count, start, end): the largest number of customers the bank can serve, and the half-open range [start, end) of the queue it serves. If several starts tie, return the earliest. If nobody can be served, return (0, 0, 0).
Constraints
0 <= len(transactions) <= 10^5-10^9 <= transactions[i] <= 10^90 <= initial <= 10^9
Example 1
initial = 1, transactions = [1, -3, 5, -2, 1]
result = (3, 2, 5)
Starting at index 0 serves one customer (balance 2), then the withdrawal of 3 would leave -1. Starting at index 2 gives balances 6, 4, 5 and serves the last three customers.
Example 2
initial = 5, transactions = [4, -10, 3, 3, -5, -6]
result = (4, 2, 6)
Starting at index 2 gives balances 8, 11, 6, 0. Ending at exactly 0 still counts.
Aim for better than O(n^2).
Clarifying questions
- If the first customer at the chosen start is a withdrawal larger than
initial, does that start serve 0 customers? - If no customer can be served, is
(0, 0, 0)returned even when the queue is empty? - On a tie in count, is the earliest start preferred even if a later start ends with a higher balance?
- Can sums overflow 64-bit integers? Values reach
10^9and there are up to10^5of them.
What a strong answer covers
- Uses prefix sums
P. Starting ats, customerjcan be served whileinitial + P[j + 1] - P[s] >= 0for every customer so far, so the run fromsends at the firstj >= swithP[j + 1] < P[s] - initial. - Finds that first index for every start fast: a sparse table of prefix minimums with a binary search (O(n log n)), or a sliding window that keeps a monotonic deque of prefix minimums as the start advances (O(n)).
- Keeps the earliest start among ties by updating the best only when a count is strictly larger.
- Edge cases: an empty queue, all deposits (the whole queue), every withdrawal too large, a balance that ends exactly at 0, and
initial = 0. - Tests: both examples, the edge cases, and a comparison with an O(n^2) brute force on random small inputs.
Follow-ups
- The bank may also skip individual customers in the middle, not just at the front.
- Return every start that achieves the maximum.
- Customers keep arriving: answer the best run so far after each one.
- The vault has a maximum too, so a deposit that would exceed it also stops service.
Where it's asked
What it tests
sliding window · two pointers · prefix sums · arrays
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
