Watch 10 video solutions for Sleep, a easy level problem. This walkthrough by NeetCodeIO has 12,972 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.
Example 1:
Input: millis = 100
Output: 100
Explanation: It should return a promise that resolves after 100ms.
let t = Date.now();
sleep(100).then(() => {
console.log(Date.now() - t); // 100
});
Example 2:
Input: millis = 200 Output: 200 Explanation: It should return a promise that resolves after 200ms.
Constraints:
1 <= millis <= 1000Problem Overview: Implement a sleep function that pauses execution for a specified number of milliseconds and resolves afterward. The function should return a promise-like asynchronous result so the caller can await it.
Approach 1: Promise with setTimeout (O(1) time, O(1) space)
The most common JavaScript implementation wraps setTimeout inside a Promise. When the function is called, you create a new promise and schedule setTimeout to call resolve after millis milliseconds. The event loop handles the delay while the main thread continues processing other tasks. This approach relies on the browser or Node.js timer system and is the standard way to implement delays in Promises-based code. Since the function only schedules a timer, the algorithmic complexity is constant: O(1) time and O(1) space.
Approach 2: Python asyncio.sleep (O(1) time, O(1) space)
Python’s asyncio.sleep provides the same behavior in an asynchronous programming environment. The function is defined with async and awaits asyncio.sleep(milliseconds / 1000). Instead of blocking the thread, the coroutine yields control back to the event loop until the timer expires. This allows other coroutines to run concurrently, which is the core advantage of asynchronous I/O. Like the JavaScript version, the operation schedules a timer and therefore runs in O(1) time and O(1) space.
Approach 3: Task.Delay / CompletableFuture Delay (O(1) time, O(1) space)
Languages with structured async frameworks provide built-in delay utilities. In C#, Task.Delay(milliseconds) returns a task that completes after the specified duration. In Java, a similar effect can be achieved using CompletableFuture with a delayed executor or scheduled task. Both rely on runtime schedulers rather than blocking the thread. The caller can await or chain the returned future, integrating naturally with concurrency primitives. Because the runtime simply schedules a timer, the complexity remains O(1) time and O(1) space.
Recommended for interviews: The JavaScript Promise + setTimeout implementation is the expected answer because it demonstrates understanding of the event loop and asynchronous control flow. Showing that you know built-in delay primitives like asyncio.sleep or Task.Delay also signals familiarity with async runtimes. The key insight interviewers look for is recognizing that you should schedule a timer rather than block execution.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Promise with setTimeout (JavaScript) | O(1) | O(1) | Standard solution in JavaScript environments using promises and the event loop |
| asyncio.sleep (Python) | O(1) | O(1) | Async Python programs using asyncio coroutines |
| Task.Delay / CompletableFuture Delay | O(1) | O(1) | C# or Java async workflows that rely on task or future scheduling |