Timeout Cancellation - Problem
Timeout Cancellation is a fundamental JavaScript concurrency problem that tests your understanding of asynchronous operations and timing control.

You need to create a cancellable timeout system. Given a function fn, an array of arguments args, and a timeout delay t in milliseconds, your task is to:

1. Return a cancel function that can stop the execution
2. Schedule fn to execute after t milliseconds
3. If the cancel function is called before t milliseconds, prevent fn from executing
4. If the cancel function is not called, execute fn with the provided args

This pattern is commonly used in debouncing, request cancellation, and cleanup operations in real-world applications.

Input & Output

example_1.js โ€” Function Executes (Not Cancelled)
$ Input: fn = (x) => x * 2, args = [5], t = 1000, cancelTimeMs = 2000
โ€บ Output: Function executes after 1000ms, outputs: 10
๐Ÿ’ก Note: Since the cancel function is called after 2000ms but the function is scheduled to execute after 1000ms, the function executes normally and returns 10.
example_2.js โ€” Function Cancelled Before Execution
$ Input: fn = () => console.log('Hello World'), args = [], t = 1000, cancelTimeMs = 500
โ€บ Output: Function never executes, no output
๐Ÿ’ก Note: The cancel function is called after 500ms, which is before the scheduled execution time of 1000ms. Therefore, the function is cancelled and never executes.
example_3.js โ€” Immediate Cancellation Edge Case
$ Input: fn = (a, b) => a + b, args = [3, 4], t = 100, cancelTimeMs = 0
โ€บ Output: Function cancelled immediately, no output
๐Ÿ’ก Note: The cancel function is called immediately (0ms), preventing the function from executing even though it has a very short 100ms timeout.

Visualization

Tap to expand
Timeout Cancellation TimelineTimeline (milliseconds)0mssetTimeout called500msDecision Point1000msScheduled TimePath A: CancelledclearTimeout() calledbefore 1000msโŒ Function never runsPath B: ExecutedNo cancellationTimer expires at 1000msโœ… Function executesKey InsightBrowser handles timing efficiently050010001500
Understanding the Visualization
1
Set Timer
setTimeout schedules function execution and returns a timer ID
2
Store ID
Keep the timer ID as a reference for potential cancellation
3
Wait or Cancel
Either the timer expires naturally or clearTimeout stops it early
4
Outcome
Function executes if timer completes, or gets cancelled if clearTimeout is called
Key Takeaway
๐ŸŽฏ Key Insight: JavaScript's setTimeout/clearTimeout provides O(1) timer management - the browser's event loop handles all the timing complexity for us efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Both setTimeout and clearTimeout are constant time operations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only stores a single timeout ID

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค t โ‰ค 104 (timeout between 1ms and 10 seconds)
  • 0 โ‰ค cancelTimeMs โ‰ค 104 (cancellation can be immediate or delayed)
  • args is a valid array that matches fn's parameter signature
  • fn is guaranteed to be a valid function
  • The environment supports setTimeout and clearTimeout
Asked in
Meta 45 Google 38 Netflix 32 Amazon 28
48.3K Views
High Frequency
~15 min Avg. Time
2.2K 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