Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.
fn.undefined.Example 1:
Input: fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
Output: [{"calls":1,"value":6}]
Explanation:
const onceFn = once(fn);
onceFn(1, 2, 3); // 6
onceFn(2, 3, 6); // undefined, fn was not called
Example 2:
Input: fn = (a,b,c) => (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]
Output: [{"calls":1,"value":140}]
Explanation:
const onceFn = once(fn);
onceFn(5, 7, 4); // 140
onceFn(2, 3, 6); // undefined, fn was not called
onceFn(4, 6, 8); // undefined, fn was not called
Constraints:
calls is a valid JSON array1 <= calls.length <= 101 <= calls[i].length <= 1002 <= JSON.stringify(calls).length <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The challenge is to make sure a function is only ever called once. The brute force method is all about initially assuming the function can be called, and then creating a mechanism to prevent it from being called a second time.
Here's how the algorithm would work step-by-step:
def allow_one_function_call(function):
function_has_been_called = False
def wrapper(*args, **kwargs):
nonlocal function_has_been_called
# Check if the function has already been called.
if function_has_been_called:
return None
# Mark the function as called before execution.
function_has_been_called = True
# Execute the original function.
result = function(*args, **kwargs)
return result
return wrapperThe goal is to ensure a function can only be executed once, no matter how many times it's called. We achieve this by using a flag to track if the function has already run. If it has, we simply return; otherwise, we execute the function and set the flag.
Here's how the algorithm would work step-by-step:
already_executed = False
def allow_one_function_call(function):
def wrapper(*args, **kwargs):
global already_executed
# Check if the function has already been executed.
if already_executed:
return
# Execute the function if it hasn't run yet.
result = function(*args, **kwargs)
# Set the flag to indicate the function has been executed.
already_executed = True
return result
return wrapper| Case | How to Handle |
|---|---|
| Null or undefined function as input | Throw an error or return a predefined value indicating invalid input. |
| Function throws an error during its execution | Catch the error and return undefined (or a specific error value), preventing program termination. |
| Function takes no arguments | The wrapper should call the function without any arguments. |
| Function has already been called | The function should return undefined if called more than once, as the 'called' flag prevents re-execution. |
| Calling returned function multiple times. | The returned function maintains state (closure over 'called'), and subsequent calls after the first return undefined. |
| Function returns null or undefined. | The wrapper correctly handles null or undefined return values, ensuring they are returned only on the initial execution. |
| Function contains side effects (e.g., modifying global variables). | The wrapper cannot prevent the side effects of the function, it can only control its execution count. |
| Function with multiple arguments. | The wrapper should accept and pass any number of arguments to the original function. |