Custom Interval Problem
Description
LeetCode Problem 2805.
Function customInterval
Given a function fn, a number delay and a number period, return a number id.
customInterval is a function that should execute the provided function fn at intervals based on a linear pattern defined by the formula delay + period * count.
The count in the formula represents the number of times the interval has been executed starting from an initial value of 0.
Function customClearInterval
Given the id. id is the returned value from the function customInterval.
customClearInterval should stop executing provided function fn at intervals.
Note: The setTimeout and setInterval functions in Node.js return an object, not a number.
Example 1:
Input: delay = 50, period = 20, cancelTime = 225
Output: [50,120,210]
Explanation:
const t = performance.now()
const result = []
const fn = () => {
result.push(Math.floor(performance.now() - t))
}
const id = customInterval(fn, delay, period)
setTimeout(() => {
customClearInterval(id)
}, 225)
50 + 20 * 0 = 50 // 50ms - 1st function call
50 + 20 * 1 = 70 // 50ms + 70ms = 120ms - 2nd function call
50 + 20 * 2 = 90 // 50ms + 70ms + 90ms = 210ms - 3rd function call
Example 2:
Input: delay = 20, period = 20, cancelTime = 150
Output: [20,60,120]
Explanation:
20 + 20 * 0 = 20 // 20ms - 1st function call
20 + 20 * 1 = 40 // 20ms + 40ms = 60ms - 2nd function call
20 + 20 * 2 = 60 // 20ms + 40ms + 60ms = 120ms - 3rd function call
Example 3:
Input: delay = 100, period = 200, cancelTime = 500
Output: [100,400]
Explanation:
100 + 200 * 0 = 100 // 100ms - 1st function call
100 + 200 * 1 = 300 // 100ms + 300ms = 400ms - 2nd function call
Constraints:
20 <= delay, period <= 25020 <= cancelTime <= 1000
Solution
Approach: Chained setTimeout with a growing delay
setInterval has a fixed period, so use setTimeout and schedule the next call from inside the previous one.
- Keep a
countstarting at 0. The next call is scheduled afterdelay + period * countmilliseconds; after runningfn, increasecountand schedule again. - Node timers are objects but we must return a number, so hand out our own increasing ids and keep a
Mapfrom id to the pending timer. customClearInterval(id)callsclearTimeouton the pending timer and removes the id. Because there is only ever one pending timer per id, this stops the chain.
Check Example 1 (delay = 50, period = 20): calls at 50, 50+70 = 120 and 120+90 = 210; the next would be at 330, but the cancel at 225 stops it.
Complexity: Time O(1) per scheduling step, space O(k) for k active intervals.
const timers = new Map();
let nextId = 1;
/**
* @param {Function} fn
* @param {number} delay
* @param {number} period
* @return {number} id
*/
function customInterval(fn, delay, period) {
const id = nextId++;
let count = 0;
const schedule = () => {
timers.set(
id,
setTimeout(() => {
fn();
count++;
schedule();
}, delay + period * count),
);
};
schedule();
return id;
}
/**
* @param {number} id
* @return {void}
*/
function customClearInterval(id) {
if (timers.has(id)) {
clearTimeout(timers.get(id));
timers.delete(id);
}
}