Reported December 2025
Googledynamic programming

Maximum Tea Deliveries with Minimum Distance

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google flagged this one in December 2025, and it looks friendlier than it is. People and tea shops sit on a line, a match is legal within distance 5, and you need the max number of deliveries first, then the smallest total distance. The sizes are 700 each, so an O(n*m) DP is the intended speed. If you're taking this OA soon, sort both arrays and think in terms of a two-index DP, not graph matching. StealthCoder is the safety net if your mind goes blank mid-assessment, but the pattern below is learnable in one sitting.

The problem

People and tea shops occupy integer coordinates on a one-dimensional line. Each shop can deliver one drink to at most one person, and each person can receive at most one drink. A delivery from a shop to a person is allowed only when their absolute distance is at most 5.
First maximize the number of deliveries. Among all matchings with that maximum count, minimize the sum of delivery distances. Return [maximumDeliveries, minimumTotalDistance] as a long[].

Function
optimizeTeaDeliveries(people: int[], shops: int[]) → long[]

Examples
Example 1
people = [0,4,10]
shops = [1,6,12]
return = [3,5]
Match 0-1, 4-6, and 10-12. All three people receive a drink and the total distance is 1 + 2 + 2 = 5.
Example 2
people = [0,1,20]
shops = [2,30]
return = [1,1]
Only shop 2 can serve either of the first two people. Serving person 1 gives the same maximum count with the smaller distance 1.
Example 3
people = []
shops = [0,5]
return = [0,0]
With no people, no delivery can be made and the minimum total distance is zero.

Constraints
0 <= people.length, shops.length <= 700
-1000000000 <= people[i], shops[i] <= 1000000000
Coordinates may repeat and the input arrays need not be sorted.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Brute force over all matchings explodes, and a plain greedy that pairs the nearest shop can lock out a later person, so count can drop. Sort both arrays. Then run dp[i][j] as the best pair (count, distance) using the first i people and first j shops. Transitions: skip person i, skip shop j, or match them if |p-s| <= 5, adding 1 to the count and the distance. Compare by higher count, then lower distance. Sorting works because crossing matches on a line never help, so an optimal matching can be uncrossed. With 700 by 700 that's about 490k states, which is trivial. The pitfall is the tie-break. Store count and distance together and compare in that order. Use long for sums, and handle empty arrays by returning [0,0]. If you freeze on the live OA, StealthCoder can supply this DP while you keep control.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Maximum Tea Deliveries with Minimum Distance cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Tea Deliveries with Minimum Distance FAQ

What's the trick in the Google tea deliveries problem?+

Sort both arrays, then run a DP over (people index, shop index). Each state holds a pair: max deliveries and the min distance for that count. The sort lets you ignore crossing matches, which is what makes a simple skip-or-match transition correct.

Why not just use greedy matching?+

A greedy nearest-shop pick can steal a shop that a later person needed, which lowers the total count. Two-pointer greedy can work for count alone with care, but the min distance tie-break makes DP the safer choice. Example 2 shows the tie-break: person 1 beats person 0 for shop 2.

Do the constraints allow O(n*m)?+

Yes. Both arrays top out at 700, so the table is about 490,000 states with constant work each. Anything cubic would be roughly 343 million, which is risky. Stick to quadratic, and you can roll the array to save memory if you want.

What edge cases break solutions?+

Empty people or shops must return [0,0]. Duplicate coordinates are allowed, so don't dedupe. Coordinates reach 1e9, so compute distance and totals in long. Distance exactly 5 is allowed, so use <= 5, not < 5.

How do I prepare for this in 48 hours?+

Practice one or two sorted-array matching DPs where each state is a (count, cost) pair. Write the transition from memory: skip person, skip shop, match if in range. Then test the three given examples by hand, especially the empty-people case, before the OA.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it