Custom Interval - Problem
Implement two functions to create and manage custom intervals:
Function customInterval: Given a function fn, a number delay and a number period, return a number id. The function should execute the provided function fn at intervals based on a linear pattern defined by the formula delay + period * count. The count represents the number of times the interval has been executed starting from an initial value of 0.
Function customClearInterval: Given the id returned from customInterval, stop executing the provided function fn at intervals.
Note: The setTimeout and setInterval functions in Node.js return an object, not a number.
Input & Output
Example 1 — Basic Custom Interval
$
Input:
delay = 100, period = 50
›
Output:
["customInterval", "customClearInterval"]
💡 Note:
Creates custom interval functions where first execution happens at 100ms, second at 150ms, third at 200ms, following the pattern delay + period * count
Example 2 — Zero Period
$
Input:
delay = 200, period = 0
›
Output:
["customInterval", "customClearInterval"]
💡 Note:
With period = 0, all executions happen at the same interval of 200ms since delay + 0 * count = 200
Example 3 — Large Period
$
Input:
delay = 50, period = 100
›
Output:
["customInterval", "customClearInterval"]
💡 Note:
Executions at 50ms, 150ms, 250ms, 350ms - rapidly increasing intervals
Constraints
- 0 ≤ delay ≤ 104
- 0 ≤ period ≤ 104
- Function fn will be provided at runtime
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code