Drill

ProblemsGo › events

How long is this event in minutes

easyeventsMathParsingGo

An event start and end are given as four-digit HHMM integers. Return the duration in minutes.

eventLengthMins(startHhmm: int, endHhmm: int) → int

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.

Solve it in Python →

Where you start

func eventLengthMins(startHhmm int, endHhmm int) int {
	
}

Worked examples

CallResult
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 Go
func eventLengthMins(startHhmm int, endHhmm int) int {
	sMin := (startHhmm/100)*60 + startHhmm%100
	eMin := (endHhmm/100)*60 + endHhmm%100
	diff := eMin - sMin
	return ((diff % 1440) + 1440) % 1440
}

The same problem in another language

More events problems in Go