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•Dynamic Programming
MediumLC 688

Knight Probability in Chessboard (LeetCode 688)

Target Frequency:AmazonGoogleMicrosoft

A knight starts on the square (row, column) of an n x n chessboard and makes exactly k moves. A knight move goes two squares in one direction and one square in the other, so from most squares it has 8 possible moves.

At every move, the knight picks one of its 8 moves with equal chance, even when that move would take it off the board. Once the knight is off the board, it stays off.

Return the probability that the knight is still on the board after it has made k moves.

Worked Examples

Example 1
Input:n = 3, k = 2, row = 0, column = 0
Output:0.0625
K
Explanation: From the corner, two of the eight moves stay on the board, to (1, 2) and (2, 1). From each of those, two of the eight moves stay on again. So the probability is 2/8 * 2/8 = 0.0625.
Example 2
Input:n = 1, k = 0, row = 0, column = 0
Output:1.0
Explanation: With no moves, the knight has not left the board, so the probability is 1.0.

⚖️Formal Constraints & Bounds

  • 1 <= n <= 25

  • 0 <= k <= 100

  • 0 <= row, column <= n - 1

Deep-Dive & Conceptual Insights

Core Insight

Give every square the probability of the knight being there after the moves made so far, and build the next step by passing each square's share, one eighth per move, to the targets on the board: the share that falls off the board is added nowhere, so the total left on the board is the answer.

Real-World Scenario & Production Applications

Any process that moves between states by chance and can be lost for good: a particle that random-walks until it leaves a region, a customer who may churn at each step, or a system whose chance of surviving a number of random failures you want to know.

Step-by-Step Execution Trace Table

Trace of Example 1: n = 3, k = 2, the knight starts on (0, 0), so dp[0][0] = 1.0. After each move, dp shows the probability of every square, row by row.

Movedp after the move (rows)Probability still on the board
1[0.0, 0.0, 0.0]; [0.0, 0.0, 0.125]; [0.0, 0.125, 0.0]0.25
2[0.03125, 0.0, 0.01562]; [0.0, 0.0, 0.0]; [0.01562, 0.0, 0.0]0.0625

After move 1, the corner's two on-board targets (1, 2) and (2, 1) hold 1/8 each. After move 2, each of them has two moves that stay on the board, so 4 * (1/8) * (1/8) = 0.0625 remains.

Code Shape
Archetype Code Shape
Python
1`dp[r][c]` is the probability that the knight is on `(r, c)` after the moves made so far; `dp[row][column] = 1.0`.
2For each of the `k` moves, build `nxt`: every square passes `dp[r][c] / 8` to each of its 8 targets that is on the board.
3A target off the board adds nothing, so its share is gone. Then `dp = nxt`.
4Return `sum(sum(line) for line in dp)`. The trap: dividing by 8 once at the end, or not at all, counts paths, not probability.

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

In a probability problem, a state does not hold a best value or a count: it holds a chance. Probability DP gives every state the probability of being there after the steps made so far, and each step spreads every state's probability over the states it can move to, weighted by how likely each move is. The answer is the total probability on the states that count.

🎲 The Analogy: Pouring Water Through Pipes

Imagine one litre of water on the knight's start square. Each step, every square pours its water equally into its 8 target squares. Water poured off the board is gone. After k steps, the water left on the board is the probability that the knight stayed on the board.

🔑 The Key Idea in the Code

nxt is built from dp and then replaces it, so a step never reads the share it has just received. The share that falls off the board is not added anywhere, which removes it for every later step.

  • Forgetting the chance of each move: each move has chance 1 / 8, so a square passes dp[r][c] / 8, not dp[r][c]. Without the division the table counts paths and the answer is far above 1.

  • Letting the knight wrap around the edge: a move off the board ends the walk. Taking nr % n keeps the knight on a torus, which gives a larger, wrong probability.

  • Updating dp in place: a square read after it received a share would pass that share on in the same step, so the knight moves twice in one step. Build nxt and replace dp after the pass.

  • Reading only the start square: the answer is the total over every square, because the knight may end anywhere on the board.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The probability that the knight is still on the board after k random moves": every move is equally likely, the board is the set of states, and a state that is left is lost for good. A question about a chance that depends on a number of steps, with the same rule at each step, points to Probability DP.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

dp[r][c] is the probability that the knight stands on (r, c) after the moves made so far without having left. One step builds nxt from dp: each square adds dp[r][c] / 8 to each of its 8 targets that is on the board. The answer is the total of the table after k steps.

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'd solve this with Probability DP. Let dp of r, c be the probability that the knight is on square r, c after the moves made so far and has never left the board. At the start it is one on the start square. For each of the k moves, I build a fresh board: every square passes its probability divided by eight to each of its eight targets that is still on the board, and a target off the board adds nothing, so that share is gone for good. After k moves, the total of all squares is the probability that the knight is still on the board. That is order K times N squared time and order N squared space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(K * N^2)

Look at the loops. The outer loop runs k times. Each pass visits every one of the n * n squares, and for each square the inner loop tries the 8 moves, each one a constant-time check and addition. That is k * n^2 * 8 steps, so time is O(K * N^2). The board is at most 25 x 25 and k at most 100, so there are about 500,000 steps.

SPACE COMPLEXITY

O(N^2)

dp and nxt are each an n x n table: O(N^2). Only two boards exist at a time, whatever k is, because nxt replaces dp after every pass. There is no recursion.

Formal Recurrence Relation

T(N,K)=K⋅N2⋅8=O(K⋅N2)T(N, K) = K \cdot N^2 \cdot 8 = O(K \cdot N^2)T(N,K)=K⋅N2⋅8=O(K⋅N2)

Derivation Progression

Steps

K passesK \text{ passes}K passes

for _ in range(k) builds the board after one more move, once for each move.

Squares per step

N2 squaresN^2 \text{ squares}N2 squares

for r and for c visit every square of the board in each pass.

Work per square

8 = O(1)

for dr, dc in moves tries 8 moves, each one a bounds check and an addition.

Total

K⋅N2⋅8=O(K⋅N2)K \cdot N^2 \cdot 8 = O(K \cdot N^2)K⋅N2⋅8=O(K⋅N2)

Steps times squares times a constant number of moves.

Variable Definitions

NNN

Board size, n

KKK

Number of moves, k

dp[r][c]dp[r][c]dp[r][c]

Probability that the knight is on square (r, c) after the moves made so far

Memory Architecture & Bounds

🟣 Call Stack

O(1): loops only, no recursion

🔵 Auxiliary Heap

O(N^2): the boards dp and nxt, two N x N tables at a time

🟢 Output Space

O(1): one real number

Boundary Best / Worst Cases

Best Case

O(K⋅N2)O(K \cdot N^2)O(K⋅N2): every pass visits every square, whatever the start square is.

Average Case

O(K⋅N2)O(K \cdot N^2)O(K⋅N2).

Worst Case

O(K⋅N2)O(K \cdot N^2)O(K⋅N2): the same count.

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "the probability that the knight is still on the board", "each move is equally likely". The state is a square, the rule is the same at every step, and a lost walk never returns, so this is Probability DP: pass each square's share to its targets, one step at a time.

CONSTRAINTS & BOUNDS

N≤25N \le 25N≤25 and K≤100K \le 100K≤100, so the table has 625625625 squares and the loops do about K⋅N2⋅8=5×105K \cdot N^2 \cdot 8 = 5 \times 10^5K⋅N2⋅8=5×105 steps: well under a millisecond in a compiled language. Following every sequence of moves is 8K8^K8K. The answer is a probability, so a double is enough; answers within 10−510^{-5}10−5 are accepted.

FAANG PRODUCTION TRAPS & EDGE CASES

A double keeps about 15 digits, and the probabilities after 100 steps can be tiny (about 10−3010^{-30}10−30 in a corner), which is still a valid double. Summing many tiny values is accurate here, because every term is non-negative. For an exact answer, use fractions, but the table then grows with 8K8^K8K in the denominators.

Theory Context•Dynamic Programming
MediumLC 688

Knight Probability in Chessboard (LeetCode 688)

Target Frequency:AmazonGoogleMicrosoft

A knight starts on the square (row, column) of an n x n chessboard and makes exactly k moves. A knight move goes two squares in one direction and one square in the other, so from most squares it has 8 possible moves.

At every move, the knight picks one of its 8 moves with equal chance, even when that move would take it off the board. Once the knight is off the board, it stays off.

Return the probability that the knight is still on the board after it has made k moves.

Worked Examples

Example 1
Input:n = 3, k = 2, row = 0, column = 0
Output:0.0625
K
Explanation: From the corner, two of the eight moves stay on the board, to (1, 2) and (2, 1). From each of those, two of the eight moves stay on again. So the probability is 2/8 * 2/8 = 0.0625.
Example 2
Input:n = 1, k = 0, row = 0, column = 0
Output:1.0
Explanation: With no moves, the knight has not left the board, so the probability is 1.0.

⚖️Formal Constraints & Bounds

  • 1 <= n <= 25

  • 0 <= k <= 100

  • 0 <= row, column <= n - 1

Deep-Dive & Conceptual Insights

Core Insight

Give every square the probability of the knight being there after the moves made so far, and build the next step by passing each square's share, one eighth per move, to the targets on the board: the share that falls off the board is added nowhere, so the total left on the board is the answer.

Real-World Scenario & Production Applications

Any process that moves between states by chance and can be lost for good: a particle that random-walks until it leaves a region, a customer who may churn at each step, or a system whose chance of surviving a number of random failures you want to know.

Step-by-Step Execution Trace Table

Trace of Example 1: n = 3, k = 2, the knight starts on (0, 0), so dp[0][0] = 1.0. After each move, dp shows the probability of every square, row by row.

Movedp after the move (rows)Probability still on the board
1[0.0, 0.0, 0.0]; [0.0, 0.0, 0.125]; [0.0, 0.125, 0.0]0.25
2[0.03125, 0.0, 0.01562]; [0.0, 0.0, 0.0]; [0.01562, 0.0, 0.0]0.0625

After move 1, the corner's two on-board targets (1, 2) and (2, 1) hold 1/8 each. After move 2, each of them has two moves that stay on the board, so 4 * (1/8) * (1/8) = 0.0625 remains.

Code Shape
Archetype Code Shape
Python
1`dp[r][c]` is the probability that the knight is on `(r, c)` after the moves made so far; `dp[row][column] = 1.0`.
2For each of the `k` moves, build `nxt`: every square passes `dp[r][c] / 8` to each of its 8 targets that is on the board.
3A target off the board adds nothing, so its share is gone. Then `dp = nxt`.
4Return `sum(sum(line) for line in dp)`. The trap: dividing by 8 once at the end, or not at all, counts paths, not probability.

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

In a probability problem, a state does not hold a best value or a count: it holds a chance. Probability DP gives every state the probability of being there after the steps made so far, and each step spreads every state's probability over the states it can move to, weighted by how likely each move is. The answer is the total probability on the states that count.

🎲 The Analogy: Pouring Water Through Pipes

Imagine one litre of water on the knight's start square. Each step, every square pours its water equally into its 8 target squares. Water poured off the board is gone. After k steps, the water left on the board is the probability that the knight stayed on the board.

🔑 The Key Idea in the Code

nxt is built from dp and then replaces it, so a step never reads the share it has just received. The share that falls off the board is not added anywhere, which removes it for every later step.

  • Forgetting the chance of each move: each move has chance 1 / 8, so a square passes dp[r][c] / 8, not dp[r][c]. Without the division the table counts paths and the answer is far above 1.

  • Letting the knight wrap around the edge: a move off the board ends the walk. Taking nr % n keeps the knight on a torus, which gives a larger, wrong probability.

  • Updating dp in place: a square read after it received a share would pass that share on in the same step, so the knight moves twice in one step. Build nxt and replace dp after the pass.

  • Reading only the start square: the answer is the total over every square, because the knight may end anywhere on the board.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The probability that the knight is still on the board after k random moves": every move is equally likely, the board is the set of states, and a state that is left is lost for good. A question about a chance that depends on a number of steps, with the same rule at each step, points to Probability DP.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

dp[r][c] is the probability that the knight stands on (r, c) after the moves made so far without having left. One step builds nxt from dp: each square adds dp[r][c] / 8 to each of its 8 targets that is on the board. The answer is the total of the table after k steps.

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'd solve this with Probability DP. Let dp of r, c be the probability that the knight is on square r, c after the moves made so far and has never left the board. At the start it is one on the start square. For each of the k moves, I build a fresh board: every square passes its probability divided by eight to each of its eight targets that is still on the board, and a target off the board adds nothing, so that share is gone for good. After k moves, the total of all squares is the probability that the knight is still on the board. That is order K times N squared time and order N squared space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(K * N^2)

Look at the loops. The outer loop runs k times. Each pass visits every one of the n * n squares, and for each square the inner loop tries the 8 moves, each one a constant-time check and addition. That is k * n^2 * 8 steps, so time is O(K * N^2). The board is at most 25 x 25 and k at most 100, so there are about 500,000 steps.

SPACE COMPLEXITY

O(N^2)

dp and nxt are each an n x n table: O(N^2). Only two boards exist at a time, whatever k is, because nxt replaces dp after every pass. There is no recursion.

Formal Recurrence Relation

T(N,K)=K⋅N2⋅8=O(K⋅N2)T(N, K) = K \cdot N^2 \cdot 8 = O(K \cdot N^2)T(N,K)=K⋅N2⋅8=O(K⋅N2)

Derivation Progression

Steps

K passesK \text{ passes}K passes

for _ in range(k) builds the board after one more move, once for each move.

Squares per step

N2 squaresN^2 \text{ squares}N2 squares

for r and for c visit every square of the board in each pass.

Work per square

8 = O(1)

for dr, dc in moves tries 8 moves, each one a bounds check and an addition.

Total

K⋅N2⋅8=O(K⋅N2)K \cdot N^2 \cdot 8 = O(K \cdot N^2)K⋅N2⋅8=O(K⋅N2)

Steps times squares times a constant number of moves.

Variable Definitions

NNN

Board size, n

KKK

Number of moves, k

dp[r][c]dp[r][c]dp[r][c]

Probability that the knight is on square (r, c) after the moves made so far

Memory Architecture & Bounds

🟣 Call Stack

O(1): loops only, no recursion

🔵 Auxiliary Heap

O(N^2): the boards dp and nxt, two N x N tables at a time

🟢 Output Space

O(1): one real number

Boundary Best / Worst Cases

Best Case

O(K⋅N2)O(K \cdot N^2)O(K⋅N2): every pass visits every square, whatever the start square is.

Average Case

O(K⋅N2)O(K \cdot N^2)O(K⋅N2).

Worst Case

O(K⋅N2)O(K \cdot N^2)O(K⋅N2): the same count.

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology

State-Space & Subproblem DAG(Directed Acyclic Graph) Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "the probability that the knight is still on the board", "each move is equally likely". The state is a square, the rule is the same at every step, and a lost walk never returns, so this is Probability DP: pass each square's share to its targets, one step at a time.

CONSTRAINTS & BOUNDS

N≤25N \le 25N≤25 and K≤100K \le 100K≤100, so the table has 625625625 squares and the loops do about K⋅N2⋅8=5×105K \cdot N^2 \cdot 8 = 5 \times 10^5K⋅N2⋅8=5×105 steps: well under a millisecond in a compiled language. Following every sequence of moves is 8K8^K8K. The answer is a probability, so a double is enough; answers within 10−510^{-5}10−5 are accepted.

FAANG PRODUCTION TRAPS & EDGE CASES

A double keeps about 15 digits, and the probabilities after 100 steps can be tiny (about 10−3010^{-30}10−30 in a corner), which is still a valid double. Summing many tiny values is accurate here, because every term is non-negative. For an exact answer, use fractions, but the table then grows with 8K8^K8K in the denominators.

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