Drill

ProblemsGo › monitoring

Should the alarm go off

mediummonitoringArraysSliding windowGo

A single spike is noise. An alert only fires when the reading stays over the limit for several samples in a row.

alarmFires(readings: list<int>, limit: int, inARow: 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.

Solve it in Python →

Where you start

func alarmFires(readings []int, limit int, inARow int) bool {
	
}

Worked examples

CallResult
alarmFires([]int{1, 5, 6, 7, 2}, 4, 3)true
alarmFires([]int{1, 5, 2, 6, 7}, 4, 3)false
alarmFires([]int{5, 6}, 4, 2)true
alarmFires([]int{4, 4, 4}, 4, 1)false

Hint

One counter, reset to zero the moment a reading comes back inside the limit.

Reference solution in Go
func alarmFires(readings []int, limit int, inARow int) bool {
	if inARow <= 0 {
		return false
	}
	run := 0
	for _, r := range readings {
		if r > limit {
			run++
		} else {
			run = 0
		}
		if run >= inARow {
			return true
		}
	}
	return false
}

The same problem in another language

More monitoring problems in Go