Practice Design Cancellable Function
Master the Design Cancellable Function LeetCode problem with undetectable real-time assistance. Get instant solutions and explanations during your coding interviews.
PhantomCodeAI generates complete solutions and debugging hints that you can use while explaining your approach, so you stay calm and in control.
Design Cancellable Function
Sometimes you have a long running task, and you may wish to cancel it before it completes. To help with this goal, write a function cancellable that accepts a generator object and returns an array of two values: a cancel function and a promise. You may assume the generator function will only yield promises. It is your function's responsibility to pass the values resolved by the promise back to the generator. If the promise rejects, your function should throw that error back to the generator. If the cancel callback is called before the generator is done, your function should throw an error back to the generator. That error should be the string "Cancelled" (Not an Error object). If the error was caught, the returned promise should resolve with the next value that was yielded or returned. Otherwise, the promise should reject with the thrown error. No more code should be executed. When the generator is done, the promise your function returned should resolve the value the generator returned. If, however, the generator throws an error, the returned promise should reject with the error. An example of how your code would be used: If instead cancel() was not called or was called after t=100ms, the promise would have resolved 5.
PhantomCodeAI will help you solve this problem in real-time during your interview
Understanding the Design Cancellable Function Problem
Let's break down this LeetCode problem and understand what makes it challenging in interview settings.
Problem Statement
Sometimes you have a long running task, and you may wish to cancel it before it completes. To help with this goal, write a function cancellable that accepts a generator object and returns an array of two values: a cancel function and a promise. You may assume the generator function will only yield promises. It is your function's responsibility to pass the values resolved by the promise back to the generator. If the promise rejects, your function should throw that error back to the generator. If the cancel callback is called before the generator is done, your function should throw an error back to the generator. That error should be the string "Cancelled" (Not an Error object). If the error was caught, the returned promise should resolve with the next value that was yielded or returned. Otherwise, the promise should reject with the thrown error. No more code should be executed. When the generator is done, the promise your function returned should resolve the value the generator returned. If, however, the generator throws an error, the returned promise should reject with the error. An example of how your code would be used: If instead cancel() was not called or was called after t=100ms, the promise would have resolved 5.
Source: Design Cancellable Function on LeetCode. Use the original task for its coding environment and submissions.
Design Cancellable Function
Related Topics
How PhantomCodeAI Helps
Get real-time assistance for Design Cancellable Function problems during coding interviews. PhantomCodeAI provides instant solutions and explanations.
Examples
# Example 1
generatorFunction = function*() { return 42; } cancelledAt = 100{"resolved": 42}# Example 2
generatorFunction = function*() { const msg = yield new Promise(res => res("Hello")); throw `Error: ${msg}`; } cancelledAt = null{"rejected": "Error: Hello"}# Example 3
generatorFunction = function*() { yield new Promise(res => setTimeout(res, 200)); return "Success"; } cancelledAt = 100{"rejected": "Cancelled"}# Example 4
generatorFunction = function*() { let result = 0; yield new Promise(res => setTimeout(res, 100)); result += yield new Promise(res => res(1)); yield new Promise(res => setTimeout(res, 100)); result += yield new Promise(res => res(1)); return result; } cancelledAt = null{"resolved": 2}# Example 5
generatorFunction = function*() { let result = 0; try { yield new Promise(res => setTimeout(res, 100)); result += yield new Promise(res => res(1)); yield new Promise(res => setTimeout(res, 100)); result += yield new Promise(res => res(1)); } catch(e) { return result; } return result; } cancelledAt = 150{"resolved": 1}# Example 6
generatorFunction = function*() { try { yield new Promise((resolve, reject) => reject("Promise Rejected")); } catch(e) { let a = yield new Promise(resolve => resolve(2)); let b = yield new Promise(resolve => resolve(2)); return a + b; }; } cancelledAt = null{"resolved": 4}Constraints
How PhantomCodeAI Helps with LeetCode Problems
Reinforce undetectability, platform compatibility, and real-time assistance to remove any doubt.
PhantomCodeAI Interface Illustration
Example conversation in the desktop app. The controls below are illustrative.
Help me review my approach and explain its trade-offs.
Start with your inputs and expected result. Compare your approach against the examples, then discuss edge cases and its time and space costs.
Undetectability Checklist
Platform Compatibility
User results and traction
Thousands of developers use PhantomCodeAI. Social proof signals that this approach helps real candidates land offers across a range of companies.
Undetectability and technical details
Our native desktop architecture avoids common detection vectors used by browser extensions. We provide a clear checklist so you can run basic checks and confirm the app will be invisible.
Platform compatibility and limitations
We work with Zoom, HackerRank, CodeSignal, CoderPad and other web-based platforms. Check the compatibility note and request a browser link if a specific desktop app is unsupported.
Frequently Asked Questions
Common questions about solving Design Cancellable Function and using PhantomCodeAI during coding interviews.
How does PhantomCodeAI help with LeetCode problems during a coding interview?
Which programming languages does PhantomCodeAI support?
Does PhantomCodeAI work with HackerRank, CodeSignal, and CoderPad?
Can PhantomCodeAI handle follow-up questions and modifications?
Is PhantomCodeAI detectable during live interviews?
Should I still practice these problems on my own first?
20+ Stealth Features. Zero Detection Cases.
The only AI interview tool with an undetectable desktop overlay, real-time audio listening, and personalized AI responses.