Problems › TypeScript › production
Find the bottleneck
On an assembly line the slowest station sets the pace for everything else, so it is the one worth fixing first.
- The bottleneck is the station taking the most seconds per unit.
- If two are equally slow, the one earlier in the line wins.
- Stations quoting zero or fewer seconds are not real measurements and are ignored.
- With nothing measurable, return null.
slowestStation(stations: list<Station>) → string?
Where you start
function slowestStation(stations: Station[]): string | null {
}
Worked examples
| Call | Result |
|---|---|
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 TypeScript
function slowestStation(stations: Station[]): string | null {
let name: string | null = null;
let worst = 0;
for (const s of stations) {
if (s.secondsPerUnit <= 0) continue;
if (s.secondsPerUnit > worst) {
worst = s.secondsPerUnit;
name = s.name;
}
}
return name;
}