Factory network cost with stage-to-stage transport
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's technical phone screen, last asked Apr 2026. It tests dynamic programming, prefix minimum and sorting.
A company is planning a chain of factories. Each factory has several candidate options, and you must pick exactly one option for every factory. The interview comes in parts; each part builds on the last.
Part 1: independent build costs
Implement min_build_cost(costs: list[list[int]]) -> int. costs[i] lists the build cost of each option for factory i. Rows may have different lengths. Return the minimum total build cost (the sum of the chosen options).
costs = [[4, 2, 7], [5], [3, 9]]
min_build_cost(costs) # 10 (2 + 5 + 3)
Part 2: stages with transport cost
Now the factories are stages of one production line, in order. Each option is a site (position, build_cost) on a straight road. Goods built at stage i are shipped to the site chosen for stage i + 1, which costs |x - y| for positions x and y.
Implement min_network_cost(stages: list[list[tuple[int, int]]]) -> int. Return the minimum of (sum of build costs of the chosen sites) + (sum of transport costs between consecutive stages). Start with three stages.
stages = [
[(0, 5), (10, 1)], # stage 0
[(3, 2), (9, 6)], # stage 1
[(4, 1)], # stage 2
]
min_network_cost(stages) # 12
# 0 -> 3 -> 4 costs 5 + 2 + 1 to build plus 3 + 1 to ship = 12.
# 10 -> 3 -> 4 also costs 12. The cheapest first site is not automatically best.
Part 3: any number of stages, many sites
Support any number of stages. The total number of sites K across all stages can be up to 10^5, positions are integers in [-10^9, 10^9] and build costs are in [0, 10^9]. A solution that compares every pair of sites between consecutive stages is too slow when one stage has many sites.
Constraints for all parts: every factory or stage has at least one option. An empty input returns 0.
Clarifying questions
- Can two stages pick sites at the same position, making transport free?
- Can build costs or distances be large enough to overflow 64 bits? (Up to 10^5 sites at 10^9 each, so totals reach about 10^14: fine in 64 bits.)
- Should I return the chosen sites as well as the cost?
- Is shipping only between consecutive stages, never skipping one?
What a strong answer covers
- Part 1: sums the minimum of each row, O(total options), and returns 0 for empty input.
- Part 2: dynamic programming by stage, where
best[j]is the cheapest cost ending at sitejof the current stage:build[j] + min over i of (prev[i] + |x_i - x_j|); shows why greedily taking the cheapest first site fails (the example's two routes tie at 12). - Part 3: removes the all-pairs step by splitting the absolute value: for sites to the right,
prev[i] - x_iplusx_j; to the left,prev[i] + x_iminusx_j; sorts both stages by position and sweeps with running minimums, O(K log K) overall. - Handles stages with one site, equal positions, negative positions and zero build costs.
- Tests the example, a case where a far cheap site wins, and a brute-force check on small random inputs.
Follow-ups
- Return the chosen site for every stage.
- Let the transport cost per unit distance differ between stages.
- Allow sites on a 2D map with Manhattan distance.
Where it's asked
- Stripe · Technical phone screen · Last asked Apr 202660 min · One realistic task in parts, in a project editor (CoderPad project mode or HackerRank Projects) with several files and tests
What it tests
dynamic programming · prefix minimum · sorting
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.
