Master the Design Cancellable Function LeetCode problem with undetectable real-time assistance. Get instant solutions and explanations during your coding interviews.
Phantom Code generates complete solutions and debugging hints that you can use while explaining your approach, so you stay calm and in control.
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.
Phantom Code will help you solve this problem in real-time during your interview
Let's break down this LeetCode problem and understand what makes it challenging in interview settings.
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.
Get real-time assistance for Design Cancellable Function problems during coding interviews. Phantom Code provides instant solutions and explanations.
—
—
generatorFunction = function*() { return 42; } cancelledAt = 100{"resolved": 42}generatorFunction = function*() { const msg = yield new Promise(res => res("Hello")); throw `Error: ${msg}`; } cancelledAt = null{"rejected": "Error: Hello"}generatorFunction = function*() { yield new Promise(res => setTimeout(res, 200)); return "Success"; } cancelledAt = 100{"rejected": "Cancelled"}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}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}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}Reinforce undetectability, platform compatibility, and real-time assistance to remove any doubt.
Watch how Phantom Code helps solve LeetCode problems during live interviews
Solve Design Cancellable Function — Sometimes you have a long running task, and you may wish to cancel it before it ...
Here's the optimal approach using the right algorithm:
Time: O(n) | Space: O(n)
Thousands of developers use Phantom Code. Social proof signals that this approach helps real candidates land offers across a range of companies.
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.
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.
Common questions about solving Design Cancellable Function and using Phantom Code during coding interviews.
The only AI interview tool with an undetectable desktop overlay, real-time audio listening, and personalized AI responses.