Time for a Line of Trucks to Cross a Weight-Limited Bridge
Last updated: November 29, 2026
Quick Overview
Trucks wait in order to cross a one-lane bridge that holds at most bridge_length trucks and max_weight total weight, and each truck takes bridge_length seconds to cross. Return the first second at which all trucks have crossed.
0
0
0 solved
Trucks wait in order to cross a one-lane bridge that holds at most bridge_length trucks and max_weight total weight, and each truck takes bridge_length seconds to cross. Return the first second at which all trucks have crossed.
An online assessment simulation in a logistics framing that Google uses to test queue-based time stepping. The interviewer looks for a correct tick model, a running weight sum, and ideally skipping idle seconds instead of ticking through them.
What the Interviewer Expects
- Model the bridge as a queue of (truck weight, exit time)
- Keep a running total weight on the bridge
- Admit the next truck only if weight and capacity both allow
- Advance time correctly, ideally jumping to the next exit event
- Explain the time complexity in terms of trucks, not seconds
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support trucks with different speeds?
- What if there are two lanes?
- How would you return each truck's exit time?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs: bridge_length (seconds to cross and max trucks at once), max_weight, and list weights. At most one truck enters per second. A truck entering at second t leaves at second t + bridge_lengt...
High-Level Design
Keep a deque of (exit_time, weight) and load. Time t starts at 0. For each truck: advance t by one (the next entry slot). Pop every truck whose exit time is <= t. If the new truck still does...