Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 1000Problem summary: 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. The first time the returned function is called, it should return the same result as fn. Every subsequent time it is called, it should return undefined.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
(a,b,c) => (a + b + c) [[1,2,3],[2,3,6]]
(a,b,c) => (a * b * c) [[5,7,4],[2,3,6],[4,6,8]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2666: Allow One Function Call
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2666: Allow One Function Call
// type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
// type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
//
// function once(fn: Function): OnceFn {
// let called = false;
// return function (...args) {
// if (!called) {
// called = true;
// return fn(...args);
// }
// };
// }
//
// /**
// * let fn = (a,b,c) => (a + b + c)
// * let onceFn = once(fn)
// *
// * onceFn(1,2,3); // 6
// * onceFn(2,3,6); // returns undefined without calling fn
// */
// Accepted solution for LeetCode #2666: Allow One Function Call
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2666: Allow One Function Call
// type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
// type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
//
// function once(fn: Function): OnceFn {
// let called = false;
// return function (...args) {
// if (!called) {
// called = true;
// return fn(...args);
// }
// };
// }
//
// /**
// * let fn = (a,b,c) => (a + b + c)
// * let onceFn = once(fn)
// *
// * onceFn(1,2,3); // 6
// * onceFn(2,3,6); // returns undefined without calling fn
// */
# Accepted solution for LeetCode #2666: Allow One Function Call
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2666: Allow One Function Call
# type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
# type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
#
# function once(fn: Function): OnceFn {
# let called = false;
# return function (...args) {
# if (!called) {
# called = true;
# return fn(...args);
# }
# };
# }
#
# /**
# * let fn = (a,b,c) => (a + b + c)
# * let onceFn = once(fn)
# *
# * onceFn(1,2,3); // 6
# * onceFn(2,3,6); // returns undefined without calling fn
# */
// Accepted solution for LeetCode #2666: Allow One Function Call
// Rust example auto-generated from ts reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2666: Allow One Function Call
// type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
// type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
//
// function once(fn: Function): OnceFn {
// let called = false;
// return function (...args) {
// if (!called) {
// called = true;
// return fn(...args);
// }
// };
// }
//
// /**
// * let fn = (a,b,c) => (a + b + c)
// * let onceFn = once(fn)
// *
// * onceFn(1,2,3); // 6
// * onceFn(2,3,6); // returns undefined without calling fn
// */
// Accepted solution for LeetCode #2666: Allow One Function Call
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
function once(fn: Function): OnceFn {
let called = false;
return function (...args) {
if (!called) {
called = true;
return fn(...args);
}
};
}
/**
* let fn = (a,b,c) => (a + b + c)
* let onceFn = once(fn)
*
* onceFn(1,2,3); // 6
* onceFn(2,3,6); // returns undefined without calling fn
*/
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.