Hi👋SpeedAlgo • Deliberate Practice & Cognitive Ergonomics for Software Engineers

An interactive algorithm mastery and technical interview preparation platform published by Hi👋WebEnterprise. Built for senior and staff software engineers preparing for rigorous coding screens at top tech companies (FAANG/MAMAA).

13 Core Algorithmic Patterns & 225 Practice Problems

  • 1. Two Pointers (16 Paradigms, 43 Problems): Opposite ends, sorted pair sums, palindrome verification, trapping rain water, 3Sum, container with most water — includes the Sliding Window and Fast & Slow Pointers paradigms (contiguous subarray invariants, longest substrings, Floyd's cycle detection, linked list middle).
  • 2. Binary Search (9 Paradigms, 14 Problems): Monotonic predicate partitioning (feasible(x)), boundary search, rotated sorted arrays, square roots, minimum ship capacity, median of two sorted arrays, matrix median on value range.
  • 3. Bit Manipulation (5 Paradigms, 11 Problems): Counting set bits, XOR cancellation, bit reversal, bitmask pairing, maximum XOR with a bitwise trie.
  • 4. Math & Geometry (8 Paradigms, 14 Problems): Sieve of Eratosthenes, integer/roman conversions, modular arithmetic, geometric simulation.
  • 5. Tree/Graph Depth-First Search (15 Paradigms, 24 Problems): Path sums, lowest common ancestor, tree diameter, validating BSTs, tree DP (House Robber III, Binary Tree Cameras).
  • 6. Tree/Graph Breadth-First Search (5 Paradigms, 12 Problems): Level-order traversals, shortest path in unweighted graphs, zig-zag traversals, rotting oranges, word ladders.
  • 7. Graphs (12 Paradigms, 22 Problems): Topological sort, cycle detection, Dijkstra's algorithm, bipartite graph validation, network delay.
  • 8. Backtracking (6 Paradigms, 14 Problems): Subsets, permutations, combinations, constraint satisfaction, pruning, N-Queens, Sudoku solver.
  • 9. Dynamic Programming (13 Paradigms, 28 Problems): Memoization vs tabulation, 0/1 knapsack, unbounded knapsack, coin change, edit distance, longest common subsequence.
  • 10. Heap / Priority Queue (9 Paradigms, 11 Problems): Running medians, top-k elements, interval scheduling, IPO, k-way merges.
  • 11. Advanced Data Structures (7 Paradigms, 17 Problems): Trie (prefix tree), Union-Find (Disjoint Set Union), LRU Cache, LFU Cache, range-sum queries with updates.
  • 12. Stack (5 Paradigms, 9 Problems): Next greater element, largest rectangle in histogram, remove k digits, decode string, min stack.
  • 13. Intervals & Greedy (2 Paradigms, 6 Problems): Merge intervals, interval scheduling, meeting rooms by sweep line, jump game.

4-Stage Deliberate Practice Framework

  1. Stage 1 (Compare & Learn): Read the pattern's mental model, loop invariant and Big-O proof, compare the pattern's template with the problem's solution side by side, and use the step-by-step debugger. The solution can also be shown in C#, Java, TypeScript, C++, Go or Rust for almost every problem.
  2. Stage 2 (Active Recall): Rebuild the solution from memory in a code editor, with an invariant checklist and optional progressive hints, and check it against a judged test suite.
  3. Stage 3 (AI Interview Coach, coming soon): A mock-interview stage still in development; for now, students go straight from Stage 2 to Stage 4.
  4. Stage 4 (Solve on Your Own): Solve a new problem on your own against a 15-minute timer; your code is graded against a test suite. Python and TypeScript run in your browser and C# runs on our server (design problems grade in Python and TypeScript).

Equipped with a spaced-repetition review hub on a fixed 7-step schedule (1 day up to 90 days), a two-pane Studio workspace, and study notes.

Pricing, Access & Commercial Terms

  • Core Curriculum: 100% Free. No credit card required.
  • Coins: New accounts get 40 free coins once, the daily claim adds +20 coins once a day, and inviting a friend earns you +25 coins when they complete their first practice step. These are the only ways to earn coins.
  • AI Coach: Each AI coach question costs 1-5 coins, priced by the server according to its size.
  • BYOK (Bring Your Own Key): AI coaching is free with your own Google Gemini, OpenAI or OpenRouter API key (BYOK).
  • Extras: Coins also unlock extras for 24 hours: a Recall hint level (1 coin), a Solve solution reveal (2), a LeetCode problem card's solutions (1) and a pattern's extra canonical problems (1).
  • Payments & Refunds: Nothing on the site is sold for money: coins can only be earned, never bought. There are no subscriptions or charges, so there is nothing to refund.
  • Platform Operator: Hi👋WebEnterprise. Support & policies at hispeedalgo.com.
Skip to main content
Hi👋SpeedAlgo

Invariant-First Algorithmic Mastery

Arena
238Items
Theory Context•Priority Queue / Heap
HardLC 480

Sliding Window Median (LeetCode 480)

Target Frequency:GoogleAmazonMicrosoft

The median of a sorted list of numbers is its middle value; when the list has an even count, there's no single middle entry, so the median is the average of the two values closest to the center.

  • For example, the median of [2,3,4] is 3.
  • For example, the median of [1,2,3,4] is (2 + 3) / 2 = 2.5.

You're given an integer array nums and a window size k. Slide a window of exactly k consecutive elements across nums, one step at a time from left to right, so that only the numbers currently inside the window are visible.

For every window position, report the window's median. An answer within 10^-5 of the true value is accepted.

Worked Examples

Example 1
Input:nums = [1,3,-1,-3,5,3,6,7], k = 3
Output:[1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
Explanation: Window position Median
[1 3 -1] -3 5 3 6 7 1 1 [3 -1 -3] 5 3 6 7 -1 1 3 [-1 -3 5] 3 6 7 -1 1 3 -1 [-3 5 3] 6 7 3 1 3 -1 -3 [5 3 6] 7 5 1 3 -1 -3 5 [3 6 7] 6
Example 2
Input:nums = [1,2,3,4,2,3,1,4,2], k = 3
Output:[2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
Explanation: The windows are [1,2,3], [2,3,4], [3,4,2], [4,2,3], [2,3,1], [3,1,4], [1,4,2], with medians 2, 3, 3, 3, 2, 3, 2.

⚖️Formal Constraints & Bounds

  • 1 <= k <= nums.length <= 105

  • -231 <= nums[i] <= 231 - 1

Deep-Dive & Conceptual Insights

Core Insight

Dual heap partitioning maintains rolling median parity in O(N log K) time.

Real-World Scenario & Production Applications

Streaming audio noise filtering, financial rolling median moving averages.

Step-by-Step Execution Trace Table

WindowElements SortedLower Heap (Small)Upper Heap (Large)Median
[1, 3, -1][-1, 1, 3][-1, 1][3]1.0
[3, -1, -3][-3, -1, 3][-3, -1][3]-1.0
[-1, -3, 5][-3, -1, 5][-3, -1][5]-1.0
[-3, 5, 3][-3, 3, 5][-3, 3][5]3.0
Code Shape
Archetype Code Shape
Python
1small = max-heap (lower half)
2large = min-heap (upper half)
3push the first k values and balance the heaps
4for each next value x:
5 mark the value that leaves the window as deleted
6 insert x and balance the heaps
7 pop deleted values off the heap tops
8 record the median

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Finding the median of a dynamic stream requires tracking the middle element(s). In a rolling window of fixed size KKK, elements both enter and leave the window at every step. Standard two-pointer sliding windows cannot compute medians because medians depend on sorted value magnitude rather than index position. We maintain two balancing heaps: a max-heap (small) holding the lower ⌊K/2⌋\lfloor K/2 \rfloor⌊K/2⌋ elements and a min-heap (large) holding the upper ⌈K/2⌉\lceil K/2 \rceil⌈K/2⌉ elements. To avoid expensive O(K)O(K)O(K) heap removals when elements slide out of the window, we employ lazy removal: out-of-window elements are marked in a hash map and pruned only when they surface at the heap roots.

🏟️ The Analogy: A Two-Sided Grain Scale with Stale Weights

Picture a balancing scale with two pans: the left pan holds the smaller half of the stones (max-heap, heaviest stone on top), and the right pan holds the larger half (min-heap, lightest stone on top). As the conveyor belt moves:

  1. An old stone falls off the belt. Instead of digging through the pan to fish it out immediately (which would take O(K)O(K)O(K) time), we tag its serial number on a "stale ledger" (hash map) and adjust our balance count.
  2. A new stone is placed into the appropriate pan.
  3. Whenever a tagged stone happens to float to the top of either pan, we simply discard it into the trash in O(log⁡K)O(\log K)O(logK) time until a valid, active stone sits at the root.
🔑 Why It Works

The balance invariant is tracked via a net scalar integer balance. When an element is inserted or marked dead, balance increments or decrements. Rebalancing shifts only active elements between heaps, guaranteeing optimal O(Nlog⁡K)O(N \log K)O(NlogK) execution without any O(K)O(K)O(K) array shifts!

  • Direct O(K)O(K)O(K) removal from binary heap: Removing evicted elements directly from standard heaps takes O(K)O(K)O(K) search time, causing O(N⋅K)O(N \cdot K)O(N⋅K) overall TLE. Use lazy removal with a frequency hash map for delayed balance pruning.

  • Stale elements at heap root: Failing to prune balance counts lazily on top of heaps when the root element was already evicted from the sliding window.

  • Off-by-one in window boundary eviction index: Evicting nums[i - k] occurs when index i >= k. Evicting prematurely at i < k corrupts initial window formation.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Dynamic median computation over a fixed-size moving window.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: len(small) == len(large) or len(small) == len(large) + 1.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Re-sorting each window: O(N · K log K) vs optimal O(N log K).

  • Heap size imbalance causing incorrect median sampling.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I maintain small (max-heap) and large (min-heap). For each element, I insert and rebalance sizes. Once window size k is reached, I sample the median from heap roots. Runs in O(N log K) time and O(K) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N log K)

Look at the execution steps: Dual balanced heaps with lazy deletion hash table for elements exiting window of size K. Each element addition and eviction costs O(log K). Total N steps.

SPACE COMPLEXITY

O(K) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Dual heaps and lazy deletion hash map store at most O(K) active and invalid elements.

Formal Recurrence Relation

T(N, K) = N · O(log K) = O(N log K)

Derivation Progression

Incoming Value Heap Push

O(log K) heap insertion

Inserts new element into dual heaps.

Lazy Outgoing Element Marking

delayed[out_val] += 1

Marks departing element in hash map in O(1).

Root Pruning & Rebalance

O(log K) amortized heap pops

Prunes invalid roots lazily and rebalances sizes.

Variable Definitions

NNN

Total number of elements in the array

KKK

Sliding window size

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(K) Dual heaps and lazy deletion hash map

🟢 Output Space

O(N - K + 1) Float median array

Boundary Best / Worst Cases

Best Case

O(Nlog⁡K)O(N \log K)O(NlogK)

Average Case

O(Nlog⁡K)O(N \log K)O(NlogK)

Worst Case

O(Nlog⁡K)O(N \log K)O(NlogK)

Binary Heap Priority Queue Tree

Binary Heap Priority Queue Tree
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "median of each sliding window of size k". Naive removal of an outgoing element from a heap is O(k)O(k)O(k), giving O(N⋅k)O(N \cdot k)O(N⋅k) overall time. A Senior SWE employs Dual Heaps with Lazy Deletion: maintain an auxiliary hash map delayed = defaultdict(int) counting elements that have exited the window, pruning them lazily only when they surface at the top of either heap!

CONSTRAINTS & BOUNDS

1≤k≤nums.length≤1051 \le k \le nums.length \le 10^51≤k≤nums.length≤105. Time budget is strictly O(Nlog⁡k)O(N \log k)O(Nlogk), scaling comfortably across 10510^5105 iterations.

FAANG PRODUCTION TRAPS & EDGE CASES

Size bookkeeping: len(small) reflects raw array size including dead elements, NOT valid window elements. Tracking small_size and large_size independently is mandatory for accurate rebalancing.

Theory Context•Priority Queue / Heap
HardLC 480

Sliding Window Median (LeetCode 480)

Target Frequency:GoogleAmazonMicrosoft

The median of a sorted list of numbers is its middle value; when the list has an even count, there's no single middle entry, so the median is the average of the two values closest to the center.

  • For example, the median of [2,3,4] is 3.
  • For example, the median of [1,2,3,4] is (2 + 3) / 2 = 2.5.

You're given an integer array nums and a window size k. Slide a window of exactly k consecutive elements across nums, one step at a time from left to right, so that only the numbers currently inside the window are visible.

For every window position, report the window's median. An answer within 10^-5 of the true value is accepted.

Worked Examples

Example 1
Input:nums = [1,3,-1,-3,5,3,6,7], k = 3
Output:[1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
Explanation: Window position Median
[1 3 -1] -3 5 3 6 7 1 1 [3 -1 -3] 5 3 6 7 -1 1 3 [-1 -3 5] 3 6 7 -1 1 3 -1 [-3 5 3] 6 7 3 1 3 -1 -3 [5 3 6] 7 5 1 3 -1 -3 5 [3 6 7] 6
Example 2
Input:nums = [1,2,3,4,2,3,1,4,2], k = 3
Output:[2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
Explanation: The windows are [1,2,3], [2,3,4], [3,4,2], [4,2,3], [2,3,1], [3,1,4], [1,4,2], with medians 2, 3, 3, 3, 2, 3, 2.

⚖️Formal Constraints & Bounds

  • 1 <= k <= nums.length <= 105

  • -231 <= nums[i] <= 231 - 1

Deep-Dive & Conceptual Insights

Core Insight

Dual heap partitioning maintains rolling median parity in O(N log K) time.

Real-World Scenario & Production Applications

Streaming audio noise filtering, financial rolling median moving averages.

Step-by-Step Execution Trace Table

WindowElements SortedLower Heap (Small)Upper Heap (Large)Median
[1, 3, -1][-1, 1, 3][-1, 1][3]1.0
[3, -1, -3][-3, -1, 3][-3, -1][3]-1.0
[-1, -3, 5][-3, -1, 5][-3, -1][5]-1.0
[-3, 5, 3][-3, 3, 5][-3, 3][5]3.0
Code Shape
Archetype Code Shape
Python
1small = max-heap (lower half)
2large = min-heap (upper half)
3push the first k values and balance the heaps
4for each next value x:
5 mark the value that leaves the window as deleted
6 insert x and balance the heaps
7 pop deleted values off the heap tops
8 record the median

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Finding the median of a dynamic stream requires tracking the middle element(s). In a rolling window of fixed size KKK, elements both enter and leave the window at every step. Standard two-pointer sliding windows cannot compute medians because medians depend on sorted value magnitude rather than index position. We maintain two balancing heaps: a max-heap (small) holding the lower ⌊K/2⌋\lfloor K/2 \rfloor⌊K/2⌋ elements and a min-heap (large) holding the upper ⌈K/2⌉\lceil K/2 \rceil⌈K/2⌉ elements. To avoid expensive O(K)O(K)O(K) heap removals when elements slide out of the window, we employ lazy removal: out-of-window elements are marked in a hash map and pruned only when they surface at the heap roots.

🏟️ The Analogy: A Two-Sided Grain Scale with Stale Weights

Picture a balancing scale with two pans: the left pan holds the smaller half of the stones (max-heap, heaviest stone on top), and the right pan holds the larger half (min-heap, lightest stone on top). As the conveyor belt moves:

  1. An old stone falls off the belt. Instead of digging through the pan to fish it out immediately (which would take O(K)O(K)O(K) time), we tag its serial number on a "stale ledger" (hash map) and adjust our balance count.
  2. A new stone is placed into the appropriate pan.
  3. Whenever a tagged stone happens to float to the top of either pan, we simply discard it into the trash in O(log⁡K)O(\log K)O(logK) time until a valid, active stone sits at the root.
🔑 Why It Works

The balance invariant is tracked via a net scalar integer balance. When an element is inserted or marked dead, balance increments or decrements. Rebalancing shifts only active elements between heaps, guaranteeing optimal O(Nlog⁡K)O(N \log K)O(NlogK) execution without any O(K)O(K)O(K) array shifts!

  • Direct O(K)O(K)O(K) removal from binary heap: Removing evicted elements directly from standard heaps takes O(K)O(K)O(K) search time, causing O(N⋅K)O(N \cdot K)O(N⋅K) overall TLE. Use lazy removal with a frequency hash map for delayed balance pruning.

  • Stale elements at heap root: Failing to prune balance counts lazily on top of heaps when the root element was already evicted from the sliding window.

  • Off-by-one in window boundary eviction index: Evicting nums[i - k] occurs when index i >= k. Evicting prematurely at i < k corrupts initial window formation.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Dynamic median computation over a fixed-size moving window.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: len(small) == len(large) or len(small) == len(large) + 1.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Re-sorting each window: O(N · K log K) vs optimal O(N log K).

  • Heap size imbalance causing incorrect median sampling.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I maintain small (max-heap) and large (min-heap). For each element, I insert and rebalance sizes. Once window size k is reached, I sample the median from heap roots. Runs in O(N log K) time and O(K) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N log K)

Look at the execution steps: Dual balanced heaps with lazy deletion hash table for elements exiting window of size K. Each element addition and eviction costs O(log K). Total N steps.

SPACE COMPLEXITY

O(K) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Dual heaps and lazy deletion hash map store at most O(K) active and invalid elements.

Formal Recurrence Relation

T(N, K) = N · O(log K) = O(N log K)

Derivation Progression

Incoming Value Heap Push

O(log K) heap insertion

Inserts new element into dual heaps.

Lazy Outgoing Element Marking

delayed[out_val] += 1

Marks departing element in hash map in O(1).

Root Pruning & Rebalance

O(log K) amortized heap pops

Prunes invalid roots lazily and rebalances sizes.

Variable Definitions

NNN

Total number of elements in the array

KKK

Sliding window size

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(K) Dual heaps and lazy deletion hash map

🟢 Output Space

O(N - K + 1) Float median array

Boundary Best / Worst Cases

Best Case

O(Nlog⁡K)O(N \log K)O(NlogK)

Average Case

O(Nlog⁡K)O(N \log K)O(NlogK)

Worst Case

O(Nlog⁡K)O(N \log K)O(NlogK)

Binary Heap Priority Queue Tree

Binary Heap Priority Queue Tree
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "median of each sliding window of size k". Naive removal of an outgoing element from a heap is O(k)O(k)O(k), giving O(N⋅k)O(N \cdot k)O(N⋅k) overall time. A Senior SWE employs Dual Heaps with Lazy Deletion: maintain an auxiliary hash map delayed = defaultdict(int) counting elements that have exited the window, pruning them lazily only when they surface at the top of either heap!

CONSTRAINTS & BOUNDS

1≤k≤nums.length≤1051 \le k \le nums.length \le 10^51≤k≤nums.length≤105. Time budget is strictly O(Nlog⁡k)O(N \log k)O(Nlogk), scaling comfortably across 10510^5105 iterations.

FAANG PRODUCTION TRAPS & EDGE CASES

Size bookkeeping: len(small) reflects raw array size including dead elements, NOT valid window elements. Tracking small_size and large_size independently is mandatory for accurate rebalancing.

© 2026 Hi👋WebEnterprise. All rights reserved.
Sitemap•llms.txt•