Throttle Simulation
MediumApp Logic (real-world)
The problem
Given event timestamps and an interval, output the times at which a throttled callback fires (leading edge — at most one call per interval). Used to limit scroll / resize handlers.
Sample input
events = [0,50,100,250,300,500], interval = 200
Sample output
0 250 500Show the solution · 8 languages
fun main() {
val events = intArrayOf(0, 50, 100, 250, 300, 500)
val interval = 200
val fires = mutableListOf<Int>()
var lastFire: Int? = null
for (t in events) {
if (lastFire == null || t - lastFire!! >= interval) {
fires.add(t)
lastFire = t
}
}
println(fires.joinToString(" "))
}