Is maintenance due yet
A machine logs the day of its last service and a service interval in days. A checker decides whether today it is time to service it again.
- Service is due once today is at least intervalDays after the last service.
- An interval of zero or less means the policy is broken: treat it as always due.
- A lastService dated after today is bad data — never due.
maintenanceDue(lastService: int, intervalDays: int, today: int) → bool
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 maintenanceDue(lastService int, intervalDays int, today int) bool {
}
Worked examples
| Call | Result |
|---|---|
maintenanceDue(100, 10, 110) | true |
maintenanceDue(100, 10, 109) | false |
maintenanceDue(100, 10, 100) | false |
maintenanceDue(100, 0, 50) | true |
Hint
Guard the two odd cases first, then compare the gap against the interval.
Reference solution in Go
func maintenanceDue(lastService int, intervalDays int, today int) bool {
if intervalDays <= 0 {
return true
}
if today < lastService {
return false
}
return today-lastService >= intervalDays
}