Debounce With Max Wait

hardcodingJavaScriptTiming

Overview

Standard debounce delays execution until a quiet period elapses. In real-world scenarios (scroll handlers, search-as-you-type), this can starve the callback indefinitely during sustained activity. The maxWait option guarantees the function fires at least once every maxWait milliseconds, even if calls keep arriving.

Given a stream of timestamped invocations and a configuration object, implement debounceMaxWait(events, wait, maxWait) that computes the exact timestamps at which the debounced function would fire.

Constraints

  • Input events are sorted in ascending order.
  • wait and maxWait are positive integers in milliseconds.
  • maxWait >= wait is always true.
  • Output timestamps must be in ascending order with no duplicates.
  • Only trailing-edge fires (no leading option for this problem).
  • Complexity target: $O(n)$.

Examples

// maxWait forces mid-burst fire.
debounceMaxWait([0, 50, 120, 180, 250, 500], 100, 200);
// → [200, 350, 600]
// Events 0–180 keep resetting the debounce timer. But maxWait: 200 forces
// a fire at t=200 (200ms after first call at t=0). Window resets.
// Event at 250: gap from 180 is 70 < wait, but elapsed from 200 is 50 < maxWait.
// Gap 250→500 exceeds wait so trailing fires at 350. Event 500 → trailing at 600.
// Natural gaps — maxWait never triggers.
debounceMaxWait([0, 250, 500], 100, 300);
// → [100, 350, 600]
// Each event spaced > wait, so each fires trailing normally.
// maxWait never kicks in because no window lasts > 300ms.
// Continuous rapid fire with periodic maxWait fires.
debounceMaxWait([0, 30, 60, 90, 130, 160, 190, 220, 400], 50, 120);
// → [120, 250, 450]
// Continuous calls 0–220 trigger maxWait at 120 and 250. Gap to 400 > 50 so trailing at 250.
// Event 400 starts new window with trailing at 450.

Notes

  • The first event in a window starts both the debounce timer (wait) and the maxWait timer.
  • When maxWait forces a fire, it closes the current window and the next event starts a new window.
  • A trailing edge fires at lastEventInWindow + wait unless maxWait fires first.
  • The fire time is always Math.min(lastEvent + wait, windowStart + maxWait).

Solution

Reveal solution

Resources

NameTopicDifficulty
Add BinaryAlgorithmseasyBalanced Binary TreeAlgorithmseasyBalanced BracketsAlgorithmseasyBinary SearchAlgorithmseasyBinary Search Tree Lowest Common AncestorAlgorithmseasyBinary Tree Maximum DepthAlgorithmseasyBinary Tree SubtreeAlgorithmseasyBit CountingAlgorithmseasyBit ReversalAlgorithmseasyCount Set Bits in a Binary NumberAlgorithmseasyDiameter of Binary TreeAlgorithmseasyFind Duplicates in ArrayAlgorithmseasyFirst Bad VersionAlgorithmseasyFlip Binary TreeAlgorithmseasyFlood FillAlgorithmseasyLinked List Detect CycleAlgorithmseasyLinked List ReversalAlgorithmseasyLinked Lists Combine Two SortedAlgorithmseasyLongest PalindromeAlgorithmseasyMiddle of the Linked ListAlgorithmseasyMost Common ElementsAlgorithmseasyOptimal Stock TradingAlgorithmseasyPair SumAlgorithmseasyRansom NoteAlgorithmseasyStaircase Climbing CombinationsAlgorithmseasyString AnagramAlgorithmseasyString PalindromeAlgorithmseasy01 MatrixAlgorithmsmediumAccounts MergeAlgorithmsmediumArray Product Excluding CurrentAlgorithmsmediumBinary Search Tree Kth Smallest ElementAlgorithmsmediumBinary Tree Level Order TraversalAlgorithmsmediumBinary Tree Rebuilding from Preorder and Inorder TraversalsAlgorithmsmediumBinary Tree Right Side ViewAlgorithmsmediumCombinations for Target SumAlgorithmsmediumCount Islands in a GridAlgorithmsmediumCourse DependencyAlgorithmsmediumDecode MessageAlgorithmsmediumDisjoint IntervalsAlgorithmsmediumDistinct Paths in GridAlgorithmsmediumEvaluate Reverse Polish NotationAlgorithmsmediumFind All Anagrams in a StringAlgorithmsmediumFind Element in Rotated ArrayAlgorithmsmediumFind the Longest Palindromic SubstringAlgorithmsmediumFind Word in GridAlgorithmsmediumGraph CloneAlgorithmsmediumGraph Count Connected ComponentsAlgorithmsmediumIs the Graph a TreeAlgorithmsmediumK Closest Points to OriginAlgorithmsmediumLetter Combinations of a Phone NumberAlgorithmsmediumLongest Increasing SubsequenceAlgorithmsmediumLongest Non-repeating SubstringAlgorithmsmediumLongest Repeating Substring After ReplacementsAlgorithmsmediumLowest Common Ancestor of a Binary TreeAlgorithmsmediumMatrix RotationAlgorithmsmediumMatrix Spiral TraversalAlgorithmsmediumMatrix ZeroingAlgorithmsmediumMaximum Sum in Contiguous ArrayAlgorithmsmediumMaximum Water Trapped Between WallsAlgorithmsmediumMerge New IntervalAlgorithmsmediumMerge Overlapping IntervalsAlgorithmsmediumMinimum Coins for ChangeAlgorithmsmediumMinimum Height TreesAlgorithmsmediumPalindromic SubstringsAlgorithmsmediumPartition Equal Subset SumAlgorithmsmediumPermutationsAlgorithmsmediumRotting OrangesAlgorithmsmediumSegment WordsAlgorithmsmediumSort ColorsAlgorithmsmediumString Anagram GroupsAlgorithmsmediumString to Integer (atoi)AlgorithmsmediumSubsetsAlgorithmsmediumSum Without AdditionAlgorithmsmediumTask CoordinationAlgorithmsmediumTrie (Prefix Tree)AlgorithmsmediumTriplet SumAlgorithmsmediumValidate Binary Search TreeAlgorithmsmediumBasic CalculatorAlgorithmshardBinary Tree EqualAlgorithmshardBinary Tree Maximum Total PathAlgorithmshardDelete Nth Node from End of Linked ListAlgorithmshardEnd of Array ReachableAlgorithmshardExtraterrestrial LanguageAlgorithmshardFind Missing Number in SequenceAlgorithmshardFind Words in GridAlgorithmshardLargest Rectangle in HistogramAlgorithmshardLinked Lists Combine K SortedAlgorithmshardLongest Common SubsequenceAlgorithmshardLongest Consecutive Number SequenceAlgorithmshardMaximum Product in Contiguous ArrayAlgorithmshardMaximum Profit in Job SchedulingAlgorithmshardMeeting CalendarAlgorithmshardMinimum Meeting Rooms NeededAlgorithmshardNeighborhood TheftAlgorithmshardNeighborhood Theft (Circular)AlgorithmshardNumber Stream MedianAlgorithmshardOcean FlowAlgorithmshardRearrange Linked ListAlgorithmshardShortest Substring Containing CharactersAlgorithmshardSmallest Element in Rotated Sorted ArrayAlgorithmshardTrapping Rain WaterAlgorithmshardWord FinderAlgorithmshardWord LadderAlgorithmshard
103 of 103 problems