Custom Interval - Problem
Custom Interval System

You need to implement a custom interval system that schedules function execution with dynamically increasing delays. Unlike the standard setInterval which uses fixed delays, your custom interval should calculate delays using the formula:

next_delay = delay + period × count

Where count starts at 0 and increments after each execution.

Implement two functions:

1. customInterval(fn, delay, period) - Returns an ID and schedules fn to execute with increasing delays
2. customClearInterval(id) - Stops the interval identified by id

Example Timeline:
• First execution: after delay + period × 0 = delay ms
• Second execution: after delay + period × 1 = delay + period ms
• Third execution: after delay + period × 2 = delay + 2×period ms

This creates a linear progression of delays, useful for implementing backoff strategies, progressive polling, or adaptive scheduling systems.

Input & Output

basic_interval.js — JavaScript
$ Input: fn = () => console.log('executed'), delay = 100, period = 50
Output: Executions at: 100ms, 250ms, 450ms, 700ms, ...
💡 Note: First execution after 100ms, then each subsequent execution has 50ms more delay (100+50×1=150ms, 100+50×2=200ms, etc.)
clear_interval.js — JavaScript
$ Input: id = customInterval(() => console.log('test'), 200, 100); customClearInterval(id);
Output: No executions (cleared before first execution)
💡 Note: The interval is cleared before the first execution at 200ms, so nothing gets printed
multiple_intervals.js — JavaScript
$ Input: Two intervals: (fn1, 100, 0) and (fn2, 50, 100)
Output: fn1 executions: 100ms, 200ms, 300ms... | fn2 executions: 50ms, 250ms, 550ms...
💡 Note: First interval has constant 100ms delay (period=0), second has increasing delays: 50, 150, 250, 350ms...

Constraints

  • 1 ≤ delay ≤ 104 (delay in milliseconds)
  • 0 ≤ period ≤ 103 (period increment in milliseconds)
  • Function fn will not throw exceptions
  • Maximum 100 concurrent intervals
  • IDs must be unique and persistent until cleared

Visualization

Tap to expand
Custom Interval Timeline (delay=100, period=50)Start0msExecute 1100ms100+50×0Execute 2250ms100+50×1Execute 3450ms100+50×2Formula BreakdownCount 0: delay + period × 0 = 100 + 50 × 0 = 100msCount 1: delay + period × 1 = 100 + 50 × 1 = 150ms (wait)Count 2: delay + period × 2 = 100 + 50 × 2 = 200ms (wait)Total elapsed: 100ms, 250ms, 450ms...Recursive setTimeout ImplementationEach execution calculates its next delay and schedules itselfsetTimeout(() => { fn(); count++; scheduleNext(); }, delay + period * count)
Understanding the Visualization
1
Initial Setup
Create interval with delay=100ms, period=50ms
2
First Execution
Execute after 100ms (100 + 50×0)
3
Second Execution
Execute after 150ms (100 + 50×1)
4
Third Execution
Execute after 200ms (100 + 50×2)
5
Pattern Emerges
Each execution has progressively longer delay
Key Takeaway
🎯 Key Insight: Instead of polling continuously, let each interval calculate and schedule its own next execution using recursive setTimeout calls. This creates precise timing with minimal system overhead.
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
34.7K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen