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•Breadth-First Search (BFS)
HardLC 773

Sliding Puzzle (LeetCode 773)

Target Frequency:GoogleAmazon

You have a 2 x 3 sliding-tile board: five numbered tiles 1-5 plus one empty slot marked 0. A move swaps the empty slot with a tile that's directly above, below, left, or right of it.

The board counts as solved only when it reads [[1,2,3],[4,5,0]].

Return the fewest moves needed to reach that solved layout from the given board, or -1 if no sequence of moves can solve it.

Worked Examples

Example 1
Input:board = [[1,2,3],[4,0,5]]
Output:1
123405
Explanation: Swap the 0 and the 5 in one move.
Example 2
Input:board = [[1,2,3],[5,4,0]]
Output:-1
123540
Explanation: No number of moves will make the board solved.
Example 3
Input:board = [[4,1,2],[5,0,3]]
Output:5
412503
Explanation: Five moves is the minimum possible here. One valid sequence of swaps: Board after move 0: [[4,1,2],[5,0,3]] Board after move 1: [[4,1,2],[0,5,3]] Board after move 2: [[0,1,2],[4,5,3]] Board after move 3: [[1,0,2],[4,5,3]] Board after move 4: [[1,2,0],[4,5,3]] Board after move 5: [[1,2,3],[4,5,0]]

⚖️Formal Constraints & Bounds

  • board.length == 2

  • board[i].length == 3

  • 0 <= board[i][j] <= 5

  • Each value board[i][j] is unique.

Deep-Dive & Conceptual Insights

Core Insight

The tile board and the deadend lock are the same shape of problem: serialize the whole state into one hashable value, precompute its neighbor rule once, then BFS.

Real-World Scenario & Production Applications

Physical sliding-tile puzzles (15-puzzle style toys) and any small finite configuration space where the goal is minimum moves between two board layouts.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Serialize the Board Into a Hashable State

A 2D board is awkward to compare and hash. Flattening it row-by-row into a single 6-character string turns 'have I already explored this exact board?' into an instant O(1) set lookup.

Mathematical Recurrence / Code Invariant
start = "".join(str(x) for row in board for x in row)
target = "123450"

Step-by-Step Execution Trace Table

Hand-Executed Step Trace (board = [[1,2,3],[4,0,5]])
MovesDequeued StateBlank IndexCandidate SwapsTarget CheckEnqueued Neighbors (Marked on Enqueue)
0"123405"4swap with 1, 3, 5 -> "103425", "120435", "123450""123450" == target!Return moves + 1 = 1 ✅
Trace Inputboard = [[1,2,3],[4,0,5]]
Expected1
Hand-Executed Step Trace (board = [[1,2,3],[4,0,5]])
MovesDequeued StateBlank IndexCandidate SwapsTarget CheckEnqueued Neighbors (Marked on Enqueue)
0"123405"4swap with 1, 3, 5 -> "103425", "120435", "123450""123450" == target!Return moves + 1 = 1 ✅
Code Shape
Archetype Code Shape
Python
1start = flatten(board)
2target = '123450'
3if start == target:
4 return 0
5queue = deque([(start, 0)])
6visited = {start}
7while queue:
8 state, moves = queue.popleft()
9 zero = state.index('0')
10 for nb in neighbors[zero]:
11 nxt = state with the tiles at zero and nb swapped
12 if nxt == target:
13 return moves + 1
14 if nxt not in visited:
15 visited.add(nxt)
16 queue.append((nxt, moves + 1))
17return -1

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

The 15-puzzle (or 2×32 \times 32×3 sliding puzzle) looks like a mechanical game, but it is an implicit graph traversal problem. A 2×32 \times 32×3 board containing digits 000 through 555 has exactly 6!=7206! = 7206!=720 possible configurations. Each unique board arrangement is a single node in a 720-node state graph. An edge exists between two nodes if swapping the empty slot (digit 000) with an adjacent tile transforms one configuration into the other. Finding the minimum moves to reach the solved target "123450" is simply unweighted BFS on this finite 720-node permutation graph.

🏟️ The Analogy: A Combination Lock with 720 Discrete Faces

Imagine a Rubik's-style puzzle box that can display 720 distinct symbols on its face. Each turn of a dial transitions the box to one of 2, 3, or 4 adjacent symbols. You want to reach the "unlocked" symbol "123450". Because there are only 720 possible configurations in the entire universe of the puzzle, you could write every configuration on an index card. Finding the shortest sequence of turns is just tracing the shortest chain of cards from your starting state to the unlocked card.

🔑 Why It Works

Serializing the board into a string enables O(1)O(1)O(1) hashing in the visited set, turning a confusing 2D matrix swap problem into a clean, deterministic graph traversal.

  • Mutating 2D grid instead of string serialization: Serializing the 2x3 board as a 6-character string (e.g. '123450') allows O(1)O(1)O(1) hashing and clean state transitions.

  • Wrong precomputed adjacency mapping: In a 2x3 board flattened to indices 0..5, index 2 can move to indices 1 and 5 (not 3, as index 3 is on the next row). Verify neighbor list for all 6 slots.

  • Unreachable target state: Some permutations of the 15-puzzle/8-puzzle are mathematically unreachable (odd parity of inversions); BFS must return -1 when queue empties.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Small finite discrete configuration space + minimum moves to a single fixed goal configuration.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: flattening the board into a string turns 'have I seen this board before?' into an O(1) set lookup, and the blank's legal swap partners depend only on grid shape, not board contents.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Comparing 2D board arrays directly instead of serializing to a string, making the visited set unusable.

  • Recomputing legal neighbor positions per board instead of precomputing them once from the fixed 2x3 shape.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I flatten the board into a 6-character string, then BFS using a precomputed adjacency table of which flattened index the blank ('0') can swap with. Mark-on-enqueue against a visited set of seen strings, and return the move count the instant a swap produces '123450'. Bounded by the 720-state permutation space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(R!)

Look at the execution steps: the board has R = 6 fixed cells, so there are at most R! = 720 distinct permutations (states). Each state generates at most 4 neighbor swaps (blank has at most 4 legal partners), and building each candidate string takes O(R) time.

SPACE COMPLEXITY

O(R!) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Visited set and BFS queue hold at most R! = 720 strings, each of length R = 6.

Formal Recurrence Relation

T(R) = R! · 4 · O(R) = O(R! · R)

Derivation Progression

State Space Bound

R! = 720 total distinct board permutations

Finite permutation graph with a fixed, tiny vertex count.

Up To 4 Neighbor Swaps

<= 4 transitions per state

The blank's precomputed adjacency list has at most 4 legal swap partners on a 2x3 grid.

String Construction

O(R) per state creation

Copies and re-joins the flattened board string per candidate swap.

Variable Definitions

RRR

Total number of cells on the board (R = 6 for a 2x3 grid)

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(R!) Visited set and BFS queue of board strings

🟢 Output Space

O(1) Moves count integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1) if the board starts already solved.

Average Case

O(R!)O(R!)O(R!).

Worst Case

O(R!)O(R!)O(R!) visiting all 720 reachable board permutations.

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: "least number of moves to solve 2x3 board". Board state serialization: flatten the 2×32 \times 32×3 grid into a 6-character string (e.g. "123450"). Each move swaps '0' with an adjacent index.

CONSTRAINTS & BOUNDS

Total state permutations: 6!=7206! = 7206!=720. Maximum states is strictly 720.

FAANG PRODUCTION TRAPS & EDGE CASES

Unsolvable boards: if BFS exhausts the queue without reaching "123450", return -1.

Theory Context•Breadth-First Search (BFS)
HardLC 773

Sliding Puzzle (LeetCode 773)

Target Frequency:GoogleAmazon

You have a 2 x 3 sliding-tile board: five numbered tiles 1-5 plus one empty slot marked 0. A move swaps the empty slot with a tile that's directly above, below, left, or right of it.

The board counts as solved only when it reads [[1,2,3],[4,5,0]].

Return the fewest moves needed to reach that solved layout from the given board, or -1 if no sequence of moves can solve it.

Worked Examples

Example 1
Input:board = [[1,2,3],[4,0,5]]
Output:1
123405
Explanation: Swap the 0 and the 5 in one move.
Example 2
Input:board = [[1,2,3],[5,4,0]]
Output:-1
123540
Explanation: No number of moves will make the board solved.
Example 3
Input:board = [[4,1,2],[5,0,3]]
Output:5
412503
Explanation: Five moves is the minimum possible here. One valid sequence of swaps: Board after move 0: [[4,1,2],[5,0,3]] Board after move 1: [[4,1,2],[0,5,3]] Board after move 2: [[0,1,2],[4,5,3]] Board after move 3: [[1,0,2],[4,5,3]] Board after move 4: [[1,2,0],[4,5,3]] Board after move 5: [[1,2,3],[4,5,0]]

⚖️Formal Constraints & Bounds

  • board.length == 2

  • board[i].length == 3

  • 0 <= board[i][j] <= 5

  • Each value board[i][j] is unique.

Deep-Dive & Conceptual Insights

Core Insight

The tile board and the deadend lock are the same shape of problem: serialize the whole state into one hashable value, precompute its neighbor rule once, then BFS.

Real-World Scenario & Production Applications

Physical sliding-tile puzzles (15-puzzle style toys) and any small finite configuration space where the goal is minimum moves between two board layouts.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Serialize the Board Into a Hashable State

A 2D board is awkward to compare and hash. Flattening it row-by-row into a single 6-character string turns 'have I already explored this exact board?' into an instant O(1) set lookup.

Mathematical Recurrence / Code Invariant
start = "".join(str(x) for row in board for x in row)
target = "123450"

Step-by-Step Execution Trace Table

Hand-Executed Step Trace (board = [[1,2,3],[4,0,5]])
MovesDequeued StateBlank IndexCandidate SwapsTarget CheckEnqueued Neighbors (Marked on Enqueue)
0"123405"4swap with 1, 3, 5 -> "103425", "120435", "123450""123450" == target!Return moves + 1 = 1 ✅
Trace Inputboard = [[1,2,3],[4,0,5]]
Expected1
Hand-Executed Step Trace (board = [[1,2,3],[4,0,5]])
MovesDequeued StateBlank IndexCandidate SwapsTarget CheckEnqueued Neighbors (Marked on Enqueue)
0"123405"4swap with 1, 3, 5 -> "103425", "120435", "123450""123450" == target!Return moves + 1 = 1 ✅
Code Shape
Archetype Code Shape
Python
1start = flatten(board)
2target = '123450'
3if start == target:
4 return 0
5queue = deque([(start, 0)])
6visited = {start}
7while queue:
8 state, moves = queue.popleft()
9 zero = state.index('0')
10 for nb in neighbors[zero]:
11 nxt = state with the tiles at zero and nb swapped
12 if nxt == target:
13 return moves + 1
14 if nxt not in visited:
15 visited.add(nxt)
16 queue.append((nxt, moves + 1))
17return -1

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

The 15-puzzle (or 2×32 \times 32×3 sliding puzzle) looks like a mechanical game, but it is an implicit graph traversal problem. A 2×32 \times 32×3 board containing digits 000 through 555 has exactly 6!=7206! = 7206!=720 possible configurations. Each unique board arrangement is a single node in a 720-node state graph. An edge exists between two nodes if swapping the empty slot (digit 000) with an adjacent tile transforms one configuration into the other. Finding the minimum moves to reach the solved target "123450" is simply unweighted BFS on this finite 720-node permutation graph.

🏟️ The Analogy: A Combination Lock with 720 Discrete Faces

Imagine a Rubik's-style puzzle box that can display 720 distinct symbols on its face. Each turn of a dial transitions the box to one of 2, 3, or 4 adjacent symbols. You want to reach the "unlocked" symbol "123450". Because there are only 720 possible configurations in the entire universe of the puzzle, you could write every configuration on an index card. Finding the shortest sequence of turns is just tracing the shortest chain of cards from your starting state to the unlocked card.

🔑 Why It Works

Serializing the board into a string enables O(1)O(1)O(1) hashing in the visited set, turning a confusing 2D matrix swap problem into a clean, deterministic graph traversal.

  • Mutating 2D grid instead of string serialization: Serializing the 2x3 board as a 6-character string (e.g. '123450') allows O(1)O(1)O(1) hashing and clean state transitions.

  • Wrong precomputed adjacency mapping: In a 2x3 board flattened to indices 0..5, index 2 can move to indices 1 and 5 (not 3, as index 3 is on the next row). Verify neighbor list for all 6 slots.

  • Unreachable target state: Some permutations of the 15-puzzle/8-puzzle are mathematically unreachable (odd parity of inversions); BFS must return -1 when queue empties.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Small finite discrete configuration space + minimum moves to a single fixed goal configuration.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: flattening the board into a string turns 'have I seen this board before?' into an O(1) set lookup, and the blank's legal swap partners depend only on grid shape, not board contents.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Comparing 2D board arrays directly instead of serializing to a string, making the visited set unusable.

  • Recomputing legal neighbor positions per board instead of precomputing them once from the fixed 2x3 shape.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I flatten the board into a 6-character string, then BFS using a precomputed adjacency table of which flattened index the blank ('0') can swap with. Mark-on-enqueue against a visited set of seen strings, and return the move count the instant a swap produces '123450'. Bounded by the 720-state permutation space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(R!)

Look at the execution steps: the board has R = 6 fixed cells, so there are at most R! = 720 distinct permutations (states). Each state generates at most 4 neighbor swaps (blank has at most 4 legal partners), and building each candidate string takes O(R) time.

SPACE COMPLEXITY

O(R!) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Visited set and BFS queue hold at most R! = 720 strings, each of length R = 6.

Formal Recurrence Relation

T(R) = R! · 4 · O(R) = O(R! · R)

Derivation Progression

State Space Bound

R! = 720 total distinct board permutations

Finite permutation graph with a fixed, tiny vertex count.

Up To 4 Neighbor Swaps

<= 4 transitions per state

The blank's precomputed adjacency list has at most 4 legal swap partners on a 2x3 grid.

String Construction

O(R) per state creation

Copies and re-joins the flattened board string per candidate swap.

Variable Definitions

RRR

Total number of cells on the board (R = 6 for a 2x3 grid)

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(R!) Visited set and BFS queue of board strings

🟢 Output Space

O(1) Moves count integer

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1) if the board starts already solved.

Average Case

O(R!)O(R!)O(R!).

Worst Case

O(R!)O(R!)O(R!) visiting all 720 reachable board permutations.

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: "least number of moves to solve 2x3 board". Board state serialization: flatten the 2×32 \times 32×3 grid into a 6-character string (e.g. "123450"). Each move swaps '0' with an adjacent index.

CONSTRAINTS & BOUNDS

Total state permutations: 6!=7206! = 7206!=720. Maximum states is strictly 720.

FAANG PRODUCTION TRAPS & EDGE CASES

Unsolvable boards: if BFS exhausts the queue without reaching "123450", return -1.

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