Bridge and torch problem
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Nvidia's technical phone screen, last asked Jan 2026. It tests greedy, dynamic programming and puzzles.
A group must cross a narrow bridge at night with one flashlight. At most two people can be on the bridge at once, anyone on the bridge needs the flashlight, and a pair walks at the slower person's speed. The flashlight cannot be thrown, so someone must carry it back after each crossing except the last. Given each person's crossing time, return the minimum total time for everyone to get across.
Interface
Unless you agree on a different interface with the interviewer, assume:
min_crossing_time(times: list[int]) -> int
Example
min_crossing_time([1, 2, 5, 10]) -> 17
# 1+2 cross (2), 1 returns (1), 5+10 cross (10), 2 returns (2), 1+2 cross (2)
min_crossing_time([1, 2, 5, 8]) -> 15
min_crossing_time([1, 5, 6, 7]) -> 20 # here the fastest person escorts each one
min_crossing_time([1, 2, 3]) -> 6
min_crossing_time([3]) -> 3
min_crossing_time([]) -> 0
Constraints
- Up to 10^5 people; times are positive integers.
Clarifying questions
- Can more than two cross at once?
- Is the flashlight required for every crossing, including returns?
- Do we need the sequence of moves or only the total?
- Can the input be empty?
What a strong answer covers
- Sort the times; while more than three people remain, move the two slowest with the cheaper of two strategies: the two fastest shuttle (
t1 + 2*t2 + tn) or the fastest escorts each (2*t1 + t(n-1) + tn); then finish the base cases of 1, 2 or 3 people. - An argument for why the two slowest should cross together or be escorted, and why greedy on these two options is optimal.
-
O(n log n)for sorting,O(n)after. - Checking against a brute-force search on small inputs.
Follow-ups
- Return the actual sequence of crossings.
- The bridge holds
kpeople at a time. - Solve it as a shortest-path search over states and compare the cost.
Where it's asked
- Nvidia · Technical phone screen · Last asked Jan 2026Live technical interview, 45 to 90 minutes; a project discussion, then a small task
What it tests
greedy · dynamic programming · puzzles
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
