FastPrepMaximize Valid Trains
Problems/Maximize Valid Trains

Maximize Valid Trains

BNSF Railway logoBNSF Railway● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report keeps the locomotive and railcar formats, the rules for a valid train, the rule that each part is used once, and the goal of the first answer in dictionary order. We only added small limits for an exact search and a model with three decimal places. Our version is about 94% the same as the reported question.

You are a train dispatcher. Locomotive i has ID locomotiveIds[i] and pull capacity pullCapacityTons[i]. Railcar j has ID railcarIds[j], weight weightTons[j], and length lengthFt[j].

A valid train uses:

  • Between one and three locomotives.
  • At least one railcar.
  • Railcars whose combined length is at most 15000 feet.
  • Railcars whose combined weight is at most the sum of the selected locomotives' pull capacities.

Each locomotive and railcar may belong to at most one train. First maximize the number of valid trains. Among solutions with the same train count, maximize the combined length of all used railcars.

Return [maximumTrainCount, maximumTotalLengthFt]. All numeric inputs have at most three digits after the decimal point, and comparisons are exact to one thousandth.

Function

maximizeValidTrains(locomotiveIds: String[], pullCapacityTons: double[], railcarIds: String[], weightTons: double[], lengthFt: double[]) → double[]

Examples

Example 1

locomotiveIds = ["L1","L2"]pullCapacityTons = [100.0,60.0]railcarIds = ["C1","C2","C3"]weightTons = [70.0,50.0,40.0]lengthFt = [5000.0,4000.0,3000.0]return = [2.0,9000.0]

Use one locomotive for railcar C1 and the other for C2. This creates two trains with total length 9000 feet. Two is optimal because only two locomotives exist.

Example 2

locomotiveIds = ["A","B"]pullCapacityTons = [40.0,50.0]railcarIds = ["X","Y"]weightTons = [80.0,60.0]lengthFt = [7000.0,6000.0]return = [1.0,7000.0]

No single locomotive can pull either railcar. Combining both locomotives can pull one railcar, and choosing X maximizes length among the one-train solutions.

Constraints

  • 1 <= locomotiveIds.length <= 6.
  • 1 <= railcarIds.length <= 8.
  • Each ID array contains unique non-empty strings.
  • pullCapacityTons.length equals locomotiveIds.length.
  • weightTons.length and lengthFt.length equal railcarIds.length.
  • Every capacity, weight, and length is positive, at most 10^6, and has at most three decimal places.

More BNSF Railway interview questions

  • Minimum Station Tracks for a Train ScheduleONSITE INTERVIEW · Seen Nov 2026
  • LRU CachePHONE SCREEN · Seen Nov 2026
  • Two SumPHONE SCREEN · Seen Nov 2026
  • Minimum Bridge TurnbacksOA · Seen Aug 2026

Similar questions at other companies

  • Optimal Account BalancingAmazon · ONSITE INTERVIEW
  • Count and Score Distinct Hat AssignmentsRoblox · ONSITE INTERVIEWRoblox interview questions
  • Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Count Eight-Neighbor Unlock PatternsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Beautiful ArrangementMathWorks · ONSITE INTERVIEWMathWorks interview questions
  • Longest Anchored Song ChainIntuit · OAIntuit interview questions
  • Maximum Words in Disjoint Grid PathsIndeed · PHONE SCREENIndeed interview questions
  • Beautiful Number OrderingsDocuSign · OADocuSign interview questions
  • N-Queens Solution CounteBay · ONSITE INTERVIEWeBay interview questions
  • N-Queens Solution CountOptimus Information · ONSITE INTERVIEWOptimus Information interview questions

BNSF Railway interview experiences

  • BNSF Railway SWE Phone Screen, Level Not Reported: 1 Experience
  • BNSF Railway Experienced SWE Interviews: 2 Experiences
All BNSF Railway interview questionsSee BNSF Railway hiring insights
Saved
class Solution {
    public double[] maximizeValidTrains(String[] locomotiveIds, double[] pullCapacityTons, String[] railcarIds, double[] weightTons, double[] lengthFt) {
      // Write your code here.
    }
}
Checking account…