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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Given a function fn and a time in milliseconds t, return a debounced version of that function.
A debounced function is a function whose execution is delayed by t milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also receive the passed parameters.
For example, let's say t = 50ms, and the function was called at 30ms, 60ms, and 100ms.
The first 2 function calls would be cancelled, and the 3rd function call would be executed at 150ms.
If instead t = 35ms, The 1st call would be cancelled, the 2nd would be executed at 95ms, and the 3rd would be executed at 135ms.
The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.
Please solve it without using lodash's _.debounce() function.
Example 1:
Input:
t = 50
calls = [
{"t": 50, inputs: [1]},
{"t": 75, inputs: [2]}
]
Output: [{"t": 125, inputs: [2]}]
Explanation:
let start = Date.now();
function log(...inputs) {
console.log([Date.now() - start, inputs ])
}
const dlog = debounce(log, 50);
setTimeout(() => dlog(1), 50);
setTimeout(() => dlog(2), 75);
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
Example 2:
Input:
t = 20
calls = [
{"t": 50, inputs: [1]},
{"t": 100, inputs: [2]}
]
Output: [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
Explanation:
The 1st call is delayed until 70ms. The inputs were (1).
The 2nd call is delayed until 120ms. The inputs were (2).
Example 3:
Input:
t = 150
calls = [
{"t": 50, inputs: [1, 2]},
{"t": 300, inputs: [3, 4]},
{"t": 300, inputs: [5, 6]}
]
Output: [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
Explanation:
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
The 2nd call is cancelled by the 3rd call
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
Constraints:
0 <= t <= 10001 <= calls.length <= 100 <= calls[i].t <= 10000 <= calls[i].inputs.length <= 10Problem summary: Given a function fn and a time in milliseconds t, return a debounced version of that function. A debounced function is a function whose execution is delayed by t milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also receive the passed parameters. For example, let's say t = 50ms, and the function was called at 30ms, 60ms, and 100ms. The first 2 function calls would be cancelled, and the 3rd function call would be executed at 150ms. If instead t = 35ms, The 1st call would be cancelled, the 2nd would be executed at 95ms, and the 3rd would be executed at 135ms. The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs. Please solve it without using lodash's _.debounce() function.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
50
[{"t":50,"inputs":[1]},{"t":75,"inputs":[2]}]20
[{"t":50,"inputs":[1]},{"t":100,"inputs":[2]}]150
[{"t":50,"inputs":[1,2]},{"t":300,"inputs":[3,4]},{"t":300,"inputs":[5,6]}]promise-time-limit)cache-with-time-limit)throttle)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2627: Debounce
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2627: Debounce
// type F = (...p: any[]) => any;
//
// function debounce(fn: F, t: number): F {
// let timeout: ReturnType<typeof setTimeout> | undefined;
//
// return function (...args) {
// if (timeout !== undefined) {
// clearTimeout(timeout);
// }
// timeout = setTimeout(() => {
// fn.apply(this, args);
// }, t);
// };
// }
//
// /**
// * const log = debounce(console.log, 100);
// * log('Hello'); // cancelled
// * log('Hello'); // cancelled
// * log('Hello'); // Logged at t=100ms
// */
// Accepted solution for LeetCode #2627: Debounce
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2627: Debounce
// type F = (...p: any[]) => any;
//
// function debounce(fn: F, t: number): F {
// let timeout: ReturnType<typeof setTimeout> | undefined;
//
// return function (...args) {
// if (timeout !== undefined) {
// clearTimeout(timeout);
// }
// timeout = setTimeout(() => {
// fn.apply(this, args);
// }, t);
// };
// }
//
// /**
// * const log = debounce(console.log, 100);
// * log('Hello'); // cancelled
// * log('Hello'); // cancelled
// * log('Hello'); // Logged at t=100ms
// */
# Accepted solution for LeetCode #2627: Debounce
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2627: Debounce
# type F = (...p: any[]) => any;
#
# function debounce(fn: F, t: number): F {
# let timeout: ReturnType<typeof setTimeout> | undefined;
#
# return function (...args) {
# if (timeout !== undefined) {
# clearTimeout(timeout);
# }
# timeout = setTimeout(() => {
# fn.apply(this, args);
# }, t);
# };
# }
#
# /**
# * const log = debounce(console.log, 100);
# * log('Hello'); // cancelled
# * log('Hello'); // cancelled
# * log('Hello'); // Logged at t=100ms
# */
// Accepted solution for LeetCode #2627: Debounce
// 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 #2627: Debounce
// type F = (...p: any[]) => any;
//
// function debounce(fn: F, t: number): F {
// let timeout: ReturnType<typeof setTimeout> | undefined;
//
// return function (...args) {
// if (timeout !== undefined) {
// clearTimeout(timeout);
// }
// timeout = setTimeout(() => {
// fn.apply(this, args);
// }, t);
// };
// }
//
// /**
// * const log = debounce(console.log, 100);
// * log('Hello'); // cancelled
// * log('Hello'); // cancelled
// * log('Hello'); // Logged at t=100ms
// */
// Accepted solution for LeetCode #2627: Debounce
type F = (...p: any[]) => any;
function debounce(fn: F, t: number): F {
let timeout: ReturnType<typeof setTimeout> | undefined;
return function (...args) {
if (timeout !== undefined) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
fn.apply(this, args);
}, t);
};
}
/**
* const log = debounce(console.log, 100);
* log('Hello'); // cancelled
* log('Hello'); // cancelled
* log('Hello'); // Logged at t=100ms
*/
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.