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?
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func slowestStation(stations []Station) *string {
}
Worked examples
| Call | Result |
|---|---|
slowestStation([]Station{Station{Name: "press", SecondsPerUnit: 12}, Station{Name: "weld", SecondsPerUnit: 30}, Station{Name: "pack", SecondsPerUnit: 9}}) | pStr("weld") |
slowestStation([]Station{Station{Name: "a", SecondsPerUnit: 20}, Station{Name: "b", SecondsPerUnit: 20}}) | pStr("a") |
slowestStation([]Station{Station{Name: "broken", SecondsPerUnit: 0}, Station{Name: "weld", SecondsPerUnit: 5}}) | pStr("weld") |
slowestStation([]Station{Station{Name: "broken", SecondsPerUnit: -1}}) | nil |
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 Go
func slowestStation(stations []Station) *string {
var name *string
worst := 0
for i := range stations {
s := stations[i]
if s.SecondsPerUnit <= 0 {
continue
}
if s.SecondsPerUnit > worst {
worst = s.SecondsPerUnit
n := s.Name
name = &n
}
}
return name
}