Minimum Locomotives to Cover a Schedule With Light Engine Moves

Last updated: November 29, 2026

Quick Overview

Given trains as (origin, destination, depart_hour, arrive_hour) and light-engine deadhead times between terminals, find the minimum number of locomotives so that every train is pulled, where one locomotive can run train j after train i if it can reach j's origin by j's departure.

Bnsf
Coding & Algorithms
Software Engineer
Bnsf
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given trains as (origin, destination, depart_hour, arrive_hour) and light-engine deadhead times between terminals, find the minimum number of locomotives so that every train is pulled, where one locomotive can run train j after train i if it can reach j's origin by j's departure.

Locomotive assignment is a core optimization problem at a railroad: idle power is expensive and every unit must be positioned for the next train. This BNSF style hard question checks whether you reduce the problem to minimum path cover in a DAG and solve it with bipartite matching rather than a greedy that fails.

What the Interviewer Expects
  • Builds a compatibility DAG between trains
  • Explains why a time-ordered greedy can be suboptimal
  • Reduces minimum path cover to n minus maximum bipartite matching
  • Implements augmenting-path matching correctly
  • States complexity and mentions Hopcroft-Karp for scale
Key Topics to Cover
DAG path cover
Bipartite matching
Augmenting paths
Scheduling
Operations research
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Some trains need two locomotives. How does the model change?
  • How would you add a required fueling or inspection stop after a certain number of hours?
  • What if light engine moves have a cost and you want to minimize locomotives first, then deadhead hours?
  • How would you make this cyclic for a weekly repeating schedule?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Each train needs exactly one locomotive. A locomotive that finishes train i at terminal dest_i at time arrive_i can take train j if arrive_i + travel(dest_i, origin_j) <= depart_j, where travel ...

High-Level Design

Draw edge i to j when one engine can do j after i. Because time strictly increases along edges, the graph is a DAG. Locomotive chains are vertex-disjoint paths covering every train, so we want a **min...


Submit Your Answer
Markdown supported

Related Questions