How long is this event in minutes
An event start and end are given as four-digit HHMM integers. Return the duration in minutes.
- 0930 means 9 hours and 30 minutes = 570 minutes.
- 2330 means 23 hours and 30 minutes = 1410 minutes.
- 2400 is a valid spelling of 24:00 = 1440 minutes.
- The duration is (end − start) minutes taken modulo 1440, so the result is always in 0..1439.
- Start and end at the same time gives 0.
eventLengthMins(startHhmm: int, endHhmm: int) → int
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.
Where you start
int eventLengthMins(int startHhmm, int endHhmm) {
}
Worked examples
| Call | Result |
|---|---|
eventLengthMins(930, 1130) | 120 |
eventLengthMins(0, 100) | 60 |
eventLengthMins(2300, 100) | 120 |
eventLengthMins(1200, 1200) | 0 |
Hint
Convert each to minutes (hh × 60 + mm); the answer is the difference mod 1440, so add 1440 when it comes out negative.
Reference solution in Java
int eventLengthMins(int startHhmm, int endHhmm) {
int sMin = (startHhmm / 100) * 60 + (startHhmm % 100);
int eMin = (endHhmm / 100) * 60 + (endHhmm % 100);
int diff = eMin - sMin;
return ((diff % 1440) + 1440) % 1440;
}