Implement a debounced search bar that limits API calls while the user types

Quick Overview

Implement a search bar class that records timestamped keystrokes and fires an API call for the latest query only after the user has been quiet for a fixed delay. It tests debounce semantics, choosing the minimal state, handling the inclusive time boundary and making sure each query fires at most once.

Implement a debounced search bar that limits API calls while the user types

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a search bar component that uses debouncing to limit the number of API calls fired while a user types. The component records each keystroke with a timestamp and only fires an API call for the latest query if no new keystroke arrives within a fixed delay window. If the user keeps typing, earlier pending queries are discarded. All timestamps are provided externally (in milliseconds) and are guaranteed to be non-decreasing. Implement a class `DebouncedSearch(delay)`, where `delay` is the debounce window in milliseconds, with the following methods: - `type(query: str, timestamp: int)`: record that the user typed `query` at the given `timestamp` (ms). Cancels any previously pending fire. - `tick(timestamp: int) -> str`: advance time to `timestamp` and return the query to fire as an API call if the debounce window has elapsed, otherwise return an empty string. - `getApiCallCount() -> int`: return the total number of API calls that have been fired so far. ```hint What to keep Ask what an older keystroke is still worth once a newer one arrives. Then decide what stops the same query from being returned by two different tick calls. ``` ### Examples Example 1: ```text Input: ["DebouncedSearch", "type", "tick", "type", "tick", "tick", "getApiCallCount"] [[300], ["a", 0], [100], ["ap", 200], [350], [550], []] Output: [null, null, "", null, "", "ap", 1] search = DebouncedSearch(300) # debounce delay = 300 ms search.type("a", 0) # pending: "a" at t=0 search.tick(100) # -> "" (only 100 ms elapsed, < 300) search.type("ap", 200) # pending overwritten: "ap" at t=200 search.tick(350) # -> "" (only 150 ms since last keystroke) search.tick(550) # -> "ap" (350 ms elapsed >= 300, fires) search.getApiCallCount() # -> 1 ``` Example 2: ```text Input: ["DebouncedSearch", "tick", "type", "tick", "tick", "type", "tick", "getApiCallCount"] [[500], [100], ["hello", 200], [600], [800], ["world", 900], [1500], []] Output: [null, "", null, "", "hello", null, "world", 2] search = DebouncedSearch(500) # debounce delay = 500 ms search.tick(100) # -> "" (nothing pending) search.type("hello", 200) # pending: "hello" at t=200 search.tick(600) # -> "" (400 ms elapsed, < 500) search.tick(800) # -> "hello" (600 ms elapsed >= 500, fires) search.type("world", 900) # pending: "world" at t=900 search.tick(1500) # -> "world" (600 ms elapsed >= 500, fires) search.getApiCallCount() # -> 2 ``` ### Constraints & Assumptions - `1 <= delay <= 10^4` milliseconds - `0 <= timestamp <= 10^9` - `1 <= len(query) <= 100` - Timestamps across calls are non-decreasing. - At most `10^4` total calls to `type`, `tick` and `getApiCallCount`. ### Clarifying Questions to Ask - If a query's delay has already passed but no `tick` ran before the next `type`, is that query dropped, or should it still count as an API call? - Is the window inclusive, so that a `tick` exactly `delay` ms after the last keystroke fires? - If `type` and `tick` arrive with the same timestamp, which one is applied first? - If the user types the same text again after it already fired, should that fire a second call? ### What a Strong Answer Covers - The state the class needs (latest query, its timestamp, whether it has fired, the call count) and why older keystrokes can be thrown away - The firing condition in `tick`, including the inclusive boundary at exactly `delay` ms - How the design makes each typed query fire at most once, however many ticks follow - Time and space cost per call - Edge cases: a tick before any keystroke, a tick at the same timestamp as a keystroke, and a keystroke that arrives after the window passed with no tick in between ### Follow-up Questions - How would you change the class so a query whose window has already passed still counts as fired when the next keystroke arrives before any tick? - A user who never pauses never triggers a search. How would you add a maximum wait so a call fires at least every N ms during continuous typing? - In a real browser component the responses come back asynchronously. How do you stop a slow response for an old query from overwriting the results of a newer one? - How would you skip the call, or answer from a cache, when the query to fire matches one that fired recently?

Overview: Implement a search bar class that records timestamped keystrokes and fires an API call for the latest query only after the user has been quiet for a fixed delay. It tests debounce semantics, choosing the minimal state, handling the inclusive time boundary and making sure each query fires at most once.

Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a search bar component that uses debouncing to limit the number of API calls fired while a user types.

The component records each keystroke with a timestamp and only fires an API call for the latest query if no new keystroke arrives within a fixed delay window. If the user keeps typing, earlier pending queries are discarded.

All timestamps are provided externally (in milliseconds) and are guaranteed to be non-decreasing.

Implement a class DebouncedSearch(delay), where delay is the debounce window in milliseconds, with the following methods:

  • type(query: str, timestamp: int) : record that the user typed query at the given timestamp (ms). Cancels any previously pending fire.
  • tick(timestamp: int) -> str : advance time to timestamp and return the query to fire as an API call if the debounce window has elapsed, otherwise return an empty string.
  • getApiCallCount() -> int : return the total number of API calls that have been fired so far.

Examples

Example 1:

Input:
["DebouncedSearch", "type",   "tick", "type",      "tick", "tick", "getApiCallCount"]
[[300],             ["a", 0], [100],  ["ap", 200], [350],  [550],  []]

Output: [null, null, "", null, "", "ap", 1]

search = DebouncedSearch(300)         # debounce delay = 300 ms
search.type("a", 0)                   # pending: "a" at t=0
search.tick(100)                      # -> ""    (only 100 ms elapsed, < 300)
search.type("ap", 200)                # pending overwritten: "ap" at t=200
search.tick(350)                      # -> ""    (only 150 ms since last keystroke)
search.tick(550)                      # -> "ap"  (350 ms elapsed >= 300, fires)
search.getApiCallCount()              # -> 1

Example 2:

Input:
["DebouncedSearch", "tick", "type",         "tick", "tick", "type",         "tick", "getApiCallCount"]
[[500],             [100],  ["hello", 200], [600],  [800],  ["world", 900], [1500], []]

Output: [null, "", null, "", "hello", null, "world", 2]

search = DebouncedSearch(500)         # debounce delay = 500 ms
search.tick(100)                      # -> ""       (nothing pending)
search.type("hello", 200)             # pending: "hello" at t=200
search.tick(600)                      # -> ""       (400 ms elapsed, < 500)
search.tick(800)                      # -> "hello"  (600 ms elapsed >= 500, fires)
search.type("world", 900)             # pending: "world" at t=900
search.tick(1500)                     # -> "world"  (600 ms elapsed >= 500, fires)
search.getApiCallCount()              # -> 2

Constraints & Assumptions

  • 1 <= delay <= 10^4 milliseconds
  • 0 <= timestamp <= 10^9
  • 1 <= len(query) <= 100
  • Timestamps across calls are non-decreasing.
  • At most 10^4 total calls to type , tick and getApiCallCount .

Clarifying Questions to Ask Guidance

  • If a query's delay has already passed but no tick ran before the next type , is that query dropped, or should it still count as an API call?
  • Is the window inclusive, so that a tick exactly delay ms after the last keystroke fires?
  • If type and tick arrive with the same timestamp, which one is applied first?
  • If the user types the same text again after it already fired, should that fire a second call?

What a Strong Answer Covers Guidance

  • The state the class needs (latest query, its timestamp, whether it has fired, the call count) and why older keystrokes can be thrown away
  • The firing condition in tick , including the inclusive boundary at exactly delay ms
  • How the design makes each typed query fire at most once, however many ticks follow
  • Time and space cost per call
  • Edge cases: a tick before any keystroke, a tick at the same timestamp as a keystroke, and a keystroke that arrives after the window passed with no tick in between

Follow-up Questions Guidance

  • How would you change the class so a query whose window has already passed still counts as fired when the next keystroke arrives before any tick?
  • A user who never pauses never triggers a search. How would you add a maximum wait so a call fires at least every N ms during continuous typing?
  • In a real browser component the responses come back asynchronously. How do you stop a slow response for an old query from overwriting the results of a newer one?
  • How would you skip the call, or answer from a cache, when the query to fire matches one that fired recently?