Drill

ProblemsJavaScript › production

Find the bottleneck

mediumproductionJavaScript

On an assembly line the slowest station sets the pace for everything else, so it is the one worth fixing first.

slowestStation(stations: list<Station>) → string?

Solve it in the editor →

Where you start

function slowestStation(stations) {
  
}

Worked examples

CallResult
slowestStation([{"name":"press","secondsPerUnit":12},{"name":"weld","secondsPerUnit":30},{"name":"pack","secondsPerUnit":9}])"weld"
slowestStation([{"name":"a","secondsPerUnit":20},{"name":"b","secondsPerUnit":20}])"a"
slowestStation([{"name":"broken","secondsPerUnit":0},{"name":"weld","secondsPerUnit":5}])"weld"
slowestStation([{"name":"broken","secondsPerUnit":-1}])null

Hint

Track the best so far and only replace it on a strictly greater time, which gives the tie-break for free.

Reference solution in JavaScript
function slowestStation(stations) {
  let name = null, worst = 0;
  for (const s of stations) {
    if (s.secondsPerUnit <= 0) continue;
    if (s.secondsPerUnit > worst) { worst = s.secondsPerUnit; name = s.name; }
  }
  return name;
}

The same problem in another language

More production problems in JavaScript