PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Grayswan Ai

Implement a debounced function (with leading option)

Last updated: Mar 29, 2026

Quick Overview

This question evaluates mastery of debouncing patterns, timer-based asynchronous control, closures, and preservation of function context and arguments in JavaScript/TypeScript, including handling of leading and trailing edge semantics.

  • easy
  • Grayswan Ai
  • Coding & Algorithms
  • Software Engineer

Implement a debounced function (with leading option)

Company: Grayswan Ai

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem Implement a `debounce` utility in JavaScript/TypeScript. Write a function: ```ts function debounce<T extends (...args: any[]) => any>( func: T, wait: number, options?: { leading?: boolean; trailing?: boolean } ): (...args: Parameters<T>) => void ``` that returns a new function `debounced`. When `debounced` is invoked repeatedly, it delays invoking `func` until **`wait` milliseconds have elapsed since the most recent call**. ### Requirements - By default, behave like classic trailing-edge debounce: - `func` runs **once**, `wait` ms after the **last** call to `debounced`. - **Follow-up:** Support *leading debounce* via `options.leading`: - If `leading: true`, invoke `func` immediately on the first call in a burst. - Subsequent calls within the `wait` window should not invoke `func` again. - If `options.trailing` is supported (optional), define behavior clearly (e.g., allow trailing call even when `leading: true`). - Preserve `this` binding and pass through the latest arguments according to your chosen semantics. ### Clarifications to handle - `wait` is a non-negative integer. - Assume a browser/Node environment with `setTimeout`/`clearTimeout`. ### Additional follow-up - State the **time and space complexity** of each call to the returned debounced function.

Quick Answer: This question evaluates mastery of debouncing patterns, timer-based asynchronous control, closures, and preservation of function context and arguments in JavaScript/TypeScript, including handling of leading and trailing edge semantics.

Grayswan Ai logo
Grayswan Ai
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

Problem

Implement a debounce utility in JavaScript/TypeScript.

Write a function:

function debounce<T extends (...args: any[]) => any>(
  func: T,
  wait: number,
  options?: { leading?: boolean; trailing?: boolean }
): (...args: Parameters<T>) => void

that returns a new function debounced. When debounced is invoked repeatedly, it delays invoking func until wait milliseconds have elapsed since the most recent call.

Requirements

  • By default, behave like classic trailing-edge debounce:
    • func runs once , wait ms after the last call to debounced .
  • Follow-up: Support leading debounce via options.leading :
    • If leading: true , invoke func immediately on the first call in a burst.
    • Subsequent calls within the wait window should not invoke func again.
  • If options.trailing is supported (optional), define behavior clearly (e.g., allow trailing call even when leading: true ).
  • Preserve this binding and pass through the latest arguments according to your chosen semantics.

Clarifications to handle

  • wait is a non-negative integer.
  • Assume a browser/Node environment with setTimeout / clearTimeout .

Additional follow-up

  • State the time and space complexity of each call to the returned debounced function.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Grayswan Ai•More Software Engineer•Grayswan Ai Software Engineer•Grayswan Ai Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.