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•Depth-First Search (DFS)
EasyLC 543

Diameter of Binary Tree (LeetCode 543)

Target Frequency:MetaAmazonGoogleMicrosoft

You're handed a binary tree's root. Somewhere in that tree there's a longest possible walk between two nodes — not necessarily passing through the root — and its diameter is simply how many edges that walk crosses. Report that edge count.

Worked Examples

Example 1
Input:root = [1,2,3,4,5]
Output:3
42513
Explanation: The path 4 -> 2 -> 1 -> 3 (or 5 -> 2 -> 1 -> 3) has 3 edges, and no path is longer.
Example 2
Input:root = [1,2]
Output:1
21
Explanation: The only path joins the two nodes with a single edge.

⚖️Formal Constraints & Bounds

  • The number of nodes in the tree is in the range [1, 104].

  • -100 <= Node.val <= 100

Deep-Dive & Conceptual Insights

Core Insight

Every path has one highest node where it bends. At each node, the longest path bending there has height(left) + height(right) edges. Record that in a global, but return only 1 + max(left, right), because a parent can extend just one straight branch.

Real-World Scenario & Production Applications

Worst-case hop count between two devices in a tree-shaped network, the longest chain between any two people in an org chart, the widest span of a river delta.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Empty Subtree Has Height 0

A null child contributes no nodes to any downward path.

Mathematical Recurrence / Code Invariant
if not node:
    return 0
Code Shape
Archetype Code Shape
Python
1if not node:
2 return 0
3left = height(node.left)
4right = height(node.right)
5diameter = max(diameter, left + right)
6return 1 + max(left, right)

Conceptual Narrative

🧠 Pattern Intuition & Real-World Analogy

Maximum Depth returns one number per subtree. Diameter keeps that exact return value but asks a second question at every node: "if the longest path bent right here, how long would it be?" The return value and the answer are different quantities, which is why the answer lives in a separate global variable.

📏 The Analogy: Measuring the Widest Span of a River Delta

A surveyor maps a river delta that splits into channels like a tree. At every fork they ask the two branches below, "how far do you reach downstream?" The widest span through this fork is the left reach plus the right reach, and the surveyor notes it if it beats the record. But when reporting upward to the next fork, a channel can only continue in one direction, so the fork reports just its longer reach plus the one step up to its parent.

🔑 The Key Idea in the Code

The two lines after the recursive calls answer two different questions. left + right is the best path that bends at this node; it goes into the global record and is never returned. 1 + max(left, right) is the best path a parent could extend; it is returned and never recorded. Mixing them up is the whole bug class of this problem.

  • Checking only the root's bend: Checking only the bend at the root misses a longer path that sits entirely inside one subtree ([1,2,null,3,4,5,null,null,6] has diameter 4, but the root's bend is only 3).

  • Counting nodes instead of edges: Counting nodes instead of edges is off by one on every input (a single node has diameter 0).

  • Returning the bent path upward: Returning left + right upward instead of 1 + max(left, right) hands the parent a path it cannot extend, because a parent can only continue down one branch.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Longest path between any two nodes, measured in edges, not required to pass through the root.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: the longest path bending at a node has exactly height(left) + height(right) edges.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

This problem's traps are in Common Pitfalls above.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I write height(node) returning 0 for None. For a real node I get the left and right heights, update a global diameter with left + right, which is the number of edges on the path that bends here, and return 1 + max(left, right). After one call on the root the global holds the answer. That's O(N) time and O(H) stack space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at height(node): each node is visited once, makes two child calls, updates diameter with left + right, and returns 1 + max(left, right) — O(1) work per node, O(N) total. Computing heights separately at every node instead would cost O(N^2) on a skewed tree.

SPACE COMPLEXITY

O(H) Auxiliary

Only the recursion stack grows, one frame per level of the current path, so auxiliary space is the tree height H. diameter is a single integer.

Formal Recurrence Relation

T(N) = 2T(N/2) + O(1) ⟹ O(N)

Derivation Progression

Postorder Visit

N node visits

Every node is entered once, after both child heights are known.

O(1) Work Per Frame

max(diameter, left + right); 1 + max(left, right)

One comparison records the bend, one returns the straight branch.

Total Time

O(N)

N frames with constant work each.

Variable Definitions

NNN

Total number of nodes in the binary tree

HHH

Tree height (log N in a balanced tree, N in a skewed tree)

Memory Architecture & Bounds

🟣 Call Stack

O(H) recursion frames

🔵 Auxiliary Heap

O(1) global best

🟢 Output Space

O(1) returns one integer

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N) time with O(log⁡N)O(\log N)O(logN) stack on a balanced tree.

Average Case

O(N)O(N)O(N) time.

Worst Case

O(N)O(N)O(N) time with O(N)O(N)O(N) stack on a skewed tree.

Graph & Tree Traversal Frontier

Graph & Tree Traversal Frontier
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "longest path between any two nodes", "may or may not pass through the root", "number of edges". Bottom-up height return with a global answer: each node returns its height, and separately updates a global best with the path that bends at it.

CONSTRAINTS & BOUNDS

N≤104N \le 10^4N≤104. One postorder pass: O(N)O(N)O(N) time.

Theory Context•Depth-First Search (DFS)
EasyLC 543

Diameter of Binary Tree (LeetCode 543)

Target Frequency:MetaAmazonGoogleMicrosoft

You're handed a binary tree's root. Somewhere in that tree there's a longest possible walk between two nodes — not necessarily passing through the root — and its diameter is simply how many edges that walk crosses. Report that edge count.

Worked Examples

Example 1
Input:root = [1,2,3,4,5]
Output:3
42513
Explanation: The path 4 -> 2 -> 1 -> 3 (or 5 -> 2 -> 1 -> 3) has 3 edges, and no path is longer.
Example 2
Input:root = [1,2]
Output:1
21
Explanation: The only path joins the two nodes with a single edge.

⚖️Formal Constraints & Bounds

  • The number of nodes in the tree is in the range [1, 104].

  • -100 <= Node.val <= 100

Deep-Dive & Conceptual Insights

Core Insight

Every path has one highest node where it bends. At each node, the longest path bending there has height(left) + height(right) edges. Record that in a global, but return only 1 + max(left, right), because a parent can extend just one straight branch.

Real-World Scenario & Production Applications

Worst-case hop count between two devices in a tree-shaped network, the longest chain between any two people in an org chart, the widest span of a river delta.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Empty Subtree Has Height 0

A null child contributes no nodes to any downward path.

Mathematical Recurrence / Code Invariant
if not node:
    return 0
Code Shape
Archetype Code Shape
Python
1if not node:
2 return 0
3left = height(node.left)
4right = height(node.right)
5diameter = max(diameter, left + right)
6return 1 + max(left, right)

Conceptual Narrative

🧠 Pattern Intuition & Real-World Analogy

Maximum Depth returns one number per subtree. Diameter keeps that exact return value but asks a second question at every node: "if the longest path bent right here, how long would it be?" The return value and the answer are different quantities, which is why the answer lives in a separate global variable.

📏 The Analogy: Measuring the Widest Span of a River Delta

A surveyor maps a river delta that splits into channels like a tree. At every fork they ask the two branches below, "how far do you reach downstream?" The widest span through this fork is the left reach plus the right reach, and the surveyor notes it if it beats the record. But when reporting upward to the next fork, a channel can only continue in one direction, so the fork reports just its longer reach plus the one step up to its parent.

🔑 The Key Idea in the Code

The two lines after the recursive calls answer two different questions. left + right is the best path that bends at this node; it goes into the global record and is never returned. 1 + max(left, right) is the best path a parent could extend; it is returned and never recorded. Mixing them up is the whole bug class of this problem.

  • Checking only the root's bend: Checking only the bend at the root misses a longer path that sits entirely inside one subtree ([1,2,null,3,4,5,null,null,6] has diameter 4, but the root's bend is only 3).

  • Counting nodes instead of edges: Counting nodes instead of edges is off by one on every input (a single node has diameter 0).

  • Returning the bent path upward: Returning left + right upward instead of 1 + max(left, right) hands the parent a path it cannot extend, because a parent can only continue down one branch.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Longest path between any two nodes, measured in edges, not required to pass through the root.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: the longest path bending at a node has exactly height(left) + height(right) edges.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

This problem's traps are in Common Pitfalls above.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I write height(node) returning 0 for None. For a real node I get the left and right heights, update a global diameter with left + right, which is the number of edges on the path that bends here, and return 1 + max(left, right). After one call on the root the global holds the answer. That's O(N) time and O(H) stack space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at height(node): each node is visited once, makes two child calls, updates diameter with left + right, and returns 1 + max(left, right) — O(1) work per node, O(N) total. Computing heights separately at every node instead would cost O(N^2) on a skewed tree.

SPACE COMPLEXITY

O(H) Auxiliary

Only the recursion stack grows, one frame per level of the current path, so auxiliary space is the tree height H. diameter is a single integer.

Formal Recurrence Relation

T(N) = 2T(N/2) + O(1) ⟹ O(N)

Derivation Progression

Postorder Visit

N node visits

Every node is entered once, after both child heights are known.

O(1) Work Per Frame

max(diameter, left + right); 1 + max(left, right)

One comparison records the bend, one returns the straight branch.

Total Time

O(N)

N frames with constant work each.

Variable Definitions

NNN

Total number of nodes in the binary tree

HHH

Tree height (log N in a balanced tree, N in a skewed tree)

Memory Architecture & Bounds

🟣 Call Stack

O(H) recursion frames

🔵 Auxiliary Heap

O(1) global best

🟢 Output Space

O(1) returns one integer

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N) time with O(log⁡N)O(\log N)O(logN) stack on a balanced tree.

Average Case

O(N)O(N)O(N) time.

Worst Case

O(N)O(N)O(N) time with O(N)O(N)O(N) stack on a skewed tree.

Graph & Tree Traversal Frontier

Graph & Tree Traversal Frontier
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "longest path between any two nodes", "may or may not pass through the root", "number of edges". Bottom-up height return with a global answer: each node returns its height, and separately updates a global best with the path that bends at it.

CONSTRAINTS & BOUNDS

N≤104N \le 10^4N≤104. One postorder pass: O(N)O(N)O(N) time.

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