Drill

ProblemsJava › machines

Is maintenance due yet

easymachinesMathJava

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.

maintenanceDue(lastService: int, intervalDays: int, today: int) → bool

Java 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.

Solve it in Python →

Where you start

boolean maintenanceDue(int lastService, int intervalDays, int today) {
    
}

Worked examples

CallResult
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 Java
boolean maintenanceDue(int lastService, int intervalDays, int today) {
    if (intervalDays <= 0) return true;
    if (today < lastService) return false;
    return today - lastService >= intervalDays;
}

The same problem in another language

More machines problems in Java