DDSA Solutions

2666. Allow One Function Call

Problem Overview

Allow One Function Call is a unknown-difficulty LeetCode problem. This is a common JavaScript pattern in coding interviews. Study the solution below and note the time and space complexity before attempting variations on your own.

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Read the solution code below and trace through it on paper before submitting. For structured interview prep, follow our 30-day study guide.

2666.ts
TypeScript
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;

function once(fn: Function): OnceFn {
  let isCalled = false;
  return function (...args) {
    if (isCalled) {
      return;
    }
    isCalled = true;
    return fn(...args);
  };
}
Was this solution helpful?

Related Problems