Which month grew the most
A board slide calls out the month with the largest jump in revenue over the month before it.
- Compare each month with the one directly before it, in the order given.
- Only increases count; a month that fell is not a jump.
- On a tie, the earlier month wins.
- Fewer than two months, or no increase anywhere, gives null.
biggestJump(points: list<Point>) → 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 biggestJump(points []Point) *string {
}
Worked examples
| Call | Result |
|---|---|
biggestJump([]Point{Point{Month: "jan", Total: 100}, Point{Month: "feb", Total: 150}, Point{Month: "mar", Total: 160}}) | pStr("feb") |
biggestJump([]Point{Point{Month: "jan", Total: 100}, Point{Month: "feb", Total: 110}, Point{Month: "mar", Total: 120}}) | pStr("feb") |
biggestJump([]Point{Point{Month: "jan", Total: 100}, Point{Month: "feb", Total: 90}}) | nil |
biggestJump([]Point{Point{Month: "jan", Total: 100}}) | nil |
Hint
Start from index 1 and keep the best strictly-greater difference, so ties keep the first one.
Reference solution in Go
func biggestJump(points []Point) *string {
var bestMonth *string
bestJump := 0
for i := 1; i < len(points); i++ {
jump := points[i].Total - points[i-1].Total
if jump > bestJump {
bestJump = jump
m := points[i].Month
bestMonth = &m
}
}
return bestMonth
}