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)
HardLC 968

Binary Tree Cameras (LeetCode 968)

Target Frequency:GoogleAmazonMicrosoft

You're placing security cameras on the nodes of the root binary tree. Each camera watches three things at once: the node it sits on, that node's parent, and that node's direct children.

Figure out the fewest cameras that still leave no node unwatched.

Worked Examples

Example 1
Input:root = [0,0,null,0,0]
Output:1
0000
Explanation: One camera on the root's child watches the root above it and both nodes below it, so all four nodes are covered.
Example 2
Input:root = [0,0,null,0,null,0,null,null,0]
Output:2
00000
Explanation: The tree is a chain of 5 nodes. One camera watches at most 3 nodes in a row, so 2 cameras are needed, for example on the 2nd and 4th nodes.

⚖️Formal Constraints & Bounds

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

  • Node.val == 0

Deep-Dive & Conceptual Insights

Core Insight

Model each subtree's status as a tri-state code instead of a number. A null child returns COVERED. A node whose child NEEDS_CAMERA must place one and return HAS_CAMERA; a node with a HAS_CAMERA child returns COVERED; otherwise it returns NEEDS_CAMERA and lets its parent decide. The root needs one final correction if it still reports NEEDS_CAMERA.

Real-World Scenario & Production Applications

Physical security camera placement in building floor plans, minimum sensor coverage for network topology monitoring.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Null Child Reports COVERED

An absent child makes no demand: it returns COVERED so a leaf's parent is never forced into an unnecessary camera.

Mathematical Recurrence / Code Invariant
if not node:
    return COVERED
Code Shape
Archetype Code Shape
Python
1if not node:
2 return COVERED
3left_state, right_state = dfs(node.left), dfs(node.right)
4if left_state == NEEDS_CAMERA or right_state == NEEDS_CAMERA:
5 cameras += 1
6 return HAS_CAMERA
7if left_state == HAS_CAMERA or right_state == HAS_CAMERA:
8 return COVERED
9return NEEDS_CAMERA

Conceptual Narrative

🧠 Pattern Intuition & Real-World Analogy

Every earlier DFS archetype in this pattern returned a number upward. This one returns a discrete status code — and the parent's entire job is to react correctly to that code, greedily, without ever looking further than one level down.

📹 The Analogy: A Building Security Manager Who Only Escalates When Forced

Imagine a security manager inspecting a building floor by floor from the ground up, deciding where to mount cameras. At each floor, they ask their two direct subordinates (the children) for a one-word status report: "I'm being watched," "I have my own camera," or "I'm not covered by anything." If either subordinate reports "not covered," the manager must immediately mount a camera on the current floor — waiting even one floor longer risks that uncovered floor never getting seen. If neither subordinate needs help but one already has a camera, the current floor is automatically watched by that camera too (cameras see one level up). Only if both subordinates are already covered without handing this floor any coverage does the current floor pass its own problem up to its own manager above.

🔑 The Key Idea in the Code

The trick is that NEEDS_CAMERA is never acted upon by the node that reports it — it is only ever acted upon by that node's parent, one level up. This one-level delay is exactly what makes the placement greedy-optimal: a node never pre-emptively places a camera "just in case," it only places one the instant a child proves it is truly necessary.

  • Naive leaf-first camera placement: Placing a camera on every leaf covers each leaf redundantly. Placing on the parent of a group of leaves is always at least as good, since one camera then covers the parent, all its children, and (via the grandparent check) contributes toward the level above.

  • Null child returning NEEDS_CAMERA instead of COVERED: If an absent child incorrectly reports NEEDS_CAMERA, every leaf's parent is forced to place an unnecessary camera. A null node must return COVERED -- it makes no demand.

  • Forgetting the root correction: The root has no parent to greedily place a camera on its behalf. If dfs(root) returns NEEDS_CAMERA, one additional camera must be added after the traversal completes, or the root itself is left unwatched.

  • Acting on NEEDS_CAMERA one level too early: The node that reports NEEDS_CAMERA must never place a camera on itself -- only its parent, one level up, is allowed to react to that status. Reacting at the wrong level breaks the greedy-optimality argument.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Minimum number of cameras to monitor a binary tree, a camera watches itself plus its parent plus its children.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: a camera is placed as low in the tree as possible -- only in reaction to a child that has proven it needs one, never pre-emptively.

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 define three states -- NEEDS_CAMERA, HAS_CAMERA, COVERED -- and a postorder helper dfs(node): a null node returns COVERED. Otherwise I recurse into both children first. If either child reports NEEDS_CAMERA, I place a camera here, increment my counter, and return HAS_CAMERA. Else if either child reports HAS_CAMERA, I return COVERED. Otherwise both children are covered but nothing watches me, so I return NEEDS_CAMERA and let my parent decide. After the traversal, if the root itself came back NEEDS_CAMERA, I add one final camera since it has no parent to rely on. Runs in O(N) time and O(H) stack space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at dfs(node): each node is visited once, makes two child calls and does at most two constant-time state comparisons before returning one of three codes. N nodes × O(1) work = O(N).

SPACE COMPLEXITY

O(H) Auxiliary

Only the recursion stack grows, one frame per level of the current root-to-node path, so auxiliary space is the tree height H. The camera counter is a single integer.

Formal Recurrence Relation

T(N) = N · O(1) = O(N)

Derivation Progression

Postorder Visit

N node visits

Every node is entered once, after both children have reported their state.

O(1) Work Per Frame

≤ 2 state comparisons

The parent reacts to two small codes; no subtree is ever re-read.

Total Time

O(N)

N frames with constant work each, plus one constant-time root correction.

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) counter and three state constants

🟢 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: "minimum number of cameras", "a camera monitors its parent, itself, and its children". Postorder Greedy Tri-State Covering: instead of returning a number, each subtree reports one of three discrete states -- NEEDS_CAMERA, HAS_CAMERA, or COVERED -- and the parent greedily reacts to that state.

CONSTRAINTS & BOUNDS

N≤1000N \le 1000N≤1000. Time budget is strictly O(N)O(N)O(N), one postorder pass with no revisits.

Theory Context•Depth-First Search (DFS)
HardLC 968

Binary Tree Cameras (LeetCode 968)

Target Frequency:GoogleAmazonMicrosoft

You're placing security cameras on the nodes of the root binary tree. Each camera watches three things at once: the node it sits on, that node's parent, and that node's direct children.

Figure out the fewest cameras that still leave no node unwatched.

Worked Examples

Example 1
Input:root = [0,0,null,0,0]
Output:1
0000
Explanation: One camera on the root's child watches the root above it and both nodes below it, so all four nodes are covered.
Example 2
Input:root = [0,0,null,0,null,0,null,null,0]
Output:2
00000
Explanation: The tree is a chain of 5 nodes. One camera watches at most 3 nodes in a row, so 2 cameras are needed, for example on the 2nd and 4th nodes.

⚖️Formal Constraints & Bounds

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

  • Node.val == 0

Deep-Dive & Conceptual Insights

Core Insight

Model each subtree's status as a tri-state code instead of a number. A null child returns COVERED. A node whose child NEEDS_CAMERA must place one and return HAS_CAMERA; a node with a HAS_CAMERA child returns COVERED; otherwise it returns NEEDS_CAMERA and lets its parent decide. The root needs one final correction if it still reports NEEDS_CAMERA.

Real-World Scenario & Production Applications

Physical security camera placement in building floor plans, minimum sensor coverage for network topology monitoring.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Null Child Reports COVERED

An absent child makes no demand: it returns COVERED so a leaf's parent is never forced into an unnecessary camera.

Mathematical Recurrence / Code Invariant
if not node:
    return COVERED
Code Shape
Archetype Code Shape
Python
1if not node:
2 return COVERED
3left_state, right_state = dfs(node.left), dfs(node.right)
4if left_state == NEEDS_CAMERA or right_state == NEEDS_CAMERA:
5 cameras += 1
6 return HAS_CAMERA
7if left_state == HAS_CAMERA or right_state == HAS_CAMERA:
8 return COVERED
9return NEEDS_CAMERA

Conceptual Narrative

🧠 Pattern Intuition & Real-World Analogy

Every earlier DFS archetype in this pattern returned a number upward. This one returns a discrete status code — and the parent's entire job is to react correctly to that code, greedily, without ever looking further than one level down.

📹 The Analogy: A Building Security Manager Who Only Escalates When Forced

Imagine a security manager inspecting a building floor by floor from the ground up, deciding where to mount cameras. At each floor, they ask their two direct subordinates (the children) for a one-word status report: "I'm being watched," "I have my own camera," or "I'm not covered by anything." If either subordinate reports "not covered," the manager must immediately mount a camera on the current floor — waiting even one floor longer risks that uncovered floor never getting seen. If neither subordinate needs help but one already has a camera, the current floor is automatically watched by that camera too (cameras see one level up). Only if both subordinates are already covered without handing this floor any coverage does the current floor pass its own problem up to its own manager above.

🔑 The Key Idea in the Code

The trick is that NEEDS_CAMERA is never acted upon by the node that reports it — it is only ever acted upon by that node's parent, one level up. This one-level delay is exactly what makes the placement greedy-optimal: a node never pre-emptively places a camera "just in case," it only places one the instant a child proves it is truly necessary.

  • Naive leaf-first camera placement: Placing a camera on every leaf covers each leaf redundantly. Placing on the parent of a group of leaves is always at least as good, since one camera then covers the parent, all its children, and (via the grandparent check) contributes toward the level above.

  • Null child returning NEEDS_CAMERA instead of COVERED: If an absent child incorrectly reports NEEDS_CAMERA, every leaf's parent is forced to place an unnecessary camera. A null node must return COVERED -- it makes no demand.

  • Forgetting the root correction: The root has no parent to greedily place a camera on its behalf. If dfs(root) returns NEEDS_CAMERA, one additional camera must be added after the traversal completes, or the root itself is left unwatched.

  • Acting on NEEDS_CAMERA one level too early: The node that reports NEEDS_CAMERA must never place a camera on itself -- only its parent, one level up, is allowed to react to that status. Reacting at the wrong level breaks the greedy-optimality argument.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Minimum number of cameras to monitor a binary tree, a camera watches itself plus its parent plus its children.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: a camera is placed as low in the tree as possible -- only in reaction to a child that has proven it needs one, never pre-emptively.

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 define three states -- NEEDS_CAMERA, HAS_CAMERA, COVERED -- and a postorder helper dfs(node): a null node returns COVERED. Otherwise I recurse into both children first. If either child reports NEEDS_CAMERA, I place a camera here, increment my counter, and return HAS_CAMERA. Else if either child reports HAS_CAMERA, I return COVERED. Otherwise both children are covered but nothing watches me, so I return NEEDS_CAMERA and let my parent decide. After the traversal, if the root itself came back NEEDS_CAMERA, I add one final camera since it has no parent to rely on. Runs in O(N) time and O(H) stack space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at dfs(node): each node is visited once, makes two child calls and does at most two constant-time state comparisons before returning one of three codes. N nodes × O(1) work = O(N).

SPACE COMPLEXITY

O(H) Auxiliary

Only the recursion stack grows, one frame per level of the current root-to-node path, so auxiliary space is the tree height H. The camera counter is a single integer.

Formal Recurrence Relation

T(N) = N · O(1) = O(N)

Derivation Progression

Postorder Visit

N node visits

Every node is entered once, after both children have reported their state.

O(1) Work Per Frame

≤ 2 state comparisons

The parent reacts to two small codes; no subtree is ever re-read.

Total Time

O(N)

N frames with constant work each, plus one constant-time root correction.

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) counter and three state constants

🟢 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: "minimum number of cameras", "a camera monitors its parent, itself, and its children". Postorder Greedy Tri-State Covering: instead of returning a number, each subtree reports one of three discrete states -- NEEDS_CAMERA, HAS_CAMERA, or COVERED -- and the parent greedily reacts to that state.

CONSTRAINTS & BOUNDS

N≤1000N \le 1000N≤1000. Time budget is strictly O(N)O(N)O(N), one postorder pass with no revisits.

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