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 1143

Longest Common Subsequence (LeetCode 1143)

Target Frequency:AmazonMetaNetflix

You're handed two strings, text1 and text2. A subsequence of a string is whatever remains after deleting zero or more of its characters while keeping the survivors in their original order -- for instance, "ace" can be pulled out of "abcde" this way. A common subsequence is a subsequence the two strings share.

Report the length of the longest subsequence they have in common, or 0 if they share none.

Worked Examples

Example 1
Input:text1 = "abcde", text2 = "ace"
Output:3
Explanation: Both strings share the subsequence "ace", giving a length of 3.
Example 2
Input:text1 = "abc", text2 = "abc"
Output:3
Explanation: Since the two strings are identical, their longest shared subsequence is the whole string "abc", length 3.
Example 3
Input:text1 = "abc", text2 = "def"
Output:0
Explanation: The two strings share no characters at all, so the answer is 0.

⚖️Formal Constraints & Bounds

  • 1 <= text1.length, text2.length <= 1000

  • text1 and text2 consist of only lowercase English characters.

Deep-Dive & Conceptual Insights

Core Insight

Defining the subproblem on string prefixes turns full comparison into a 2-D recurrence: match extends diagonal by 1; mismatch takes the maximum of dropping the current character from either string.

Real-World Scenario & Production Applications

Git diff algorithm (Myers diff), DNA sequence alignment in computational genomics (BLAST/Smith-Waterman), and fuzzy text similarity.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: 2D Subproblem Matrix Setup

Define dp[i][j] as the length of the Longest Common Subsequence between prefix text1[:i] and prefix text2[:j]. Empty prefixes initialize to 0 (dp[0][j] = 0, dp[i][0] = 0).

Mathematical Recurrence / Code Invariant
# dp[i][j] = LCS length of text1[:i] and text2[:j]
dp = [[0] * (N + 1) for _ in range(M + 1)]

Step-by-Step Execution Trace Table

dp[i][j]""ace
""0000
a01 (diagonal match)11
b0111
c012 (diagonal match)2
d0122
e0123 (diagonal match →\to→ Final Answer)
Code Shape
Archetype Code Shape
Python
1dp = [[0] * (n + 1) for _ in range(m + 1)]
2for i in range(1, m + 1):
3 for j in range(1, n + 1):
4 if text1[i-1] == text2[j-1]:
5 dp[i][j] = dp[i-1][j-1] + 1
6 else:
7 dp[i][j] = max(dp[i-1][j], dp[i][j-1])
8return dp[m][n]

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition
Code / Blueprint
Longest Common Subsequence aligns two strings $s_1$ and $s_2$. At cell $(i, j)$ of the 2D DP matrix, if characters match (`s_1[i-1] == s_2[j-1]`), the alignment extends diagonally: $dp[i][j] = dp[i-1][j-1] + 1$. If they differ, the optimal alignment discards one character from either string: $dp[i][j] = max(dp[i-1][j], dp[i][j-1])$. This evaluates all prefix subproblems in $O(M \times N)$ time and $O(min(M, N))$ space.
 
🧩 The Analogy: Aligning Two Rows of Puzzle Pieces

Imagine laying two strings out as two rows of labeled tiles, and you're building an alignment table cell by cell. At cell (i, j), you're asking: "what's the longest common subsequence between the first i letters of text1 and the first j letters of text2?" If the two letters at this position match, you get to extend the alignment diagonally — the answer here is 1 more than whatever the alignment was before both letters were added (dp[i-1][j-1]). If they don't match, you can't extend a match here at all, so you inherit the best alignment achievable by dropping just one letter from either string — whichever of dp[i-1][j] or dp[i][j-1] is bigger.

🔑 The Key Idea in the Code

A match always moves diagonally (both strings advance together); a mismatch always moves orthogonally (only one string advances) — this is why LCS's DP table is famously visualized with diagonal arrows tracing the matched characters.

  • 1-based DP table coordinate shift off-by-one: Forgetting that table cell dp[i][j] maps to string indices text1[i - 1] and text2[j - 1]. Comparing text1[i] == text2[j] causes string out-of-bounds or misaligned character comparisons.

  • Mismatch transition cost: Adding +1+1+1 on mismatch states: 1 + max(dp[i-1][j], dp[i][j-1]). Mismatch transitions carry no reward; the recurrence is strictly max(dp[i-1][j], dp[i][j-1]).

  • Diagonal overwrite in 1D space optimization: In rolling 1D space, updating dp[j] overwrites the value from the previous row that dp[j+1] needs as its top-left diagonal neighbor dp[i-1][j-1]. A temporary variable prev must store the diagonal value before overwriting.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Subsequence alignment between two strings + ordering preserved + finding longest shared elements.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Recurrence: if s1[i-1] == s2[j-1]: dp[i][j] = 1 + dp[i-1][j-1]; else dp[i][j] = max(dp[i-1][j], dp[i][j-1]).

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Off-by-one indexing: grid dimensions must be (M+1) x (N+1) so row 0 and col 0 represent empty string prefixes.

  • Allocating 2D grid with shallow copies (e.g. [[0]*(N+1)]*(M+1) in Python) which causes mutated rows.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I use a 2D DP table of size (M+1) x (N+1). When characters match, I extend the optimal prefix alignment from dp[i-1][j-1] by 1. On mismatch, I take the maximum of dropping a character from text1 or text2. This yields an optimal O(M * N) time and space solution."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(M · N)

Look at the execution steps: 2D DP table of size M x N where text1 has length M and text2 has length N. Transition: if characters match, 1 + diag; else max(top, left). Rolling rows reduces space to O(min(M, N)).

SPACE COMPLEXITY

O(min(M, N)) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Two rows of size min(M, N) + 1.

Formal Recurrence Relation

T(M, N) = M · N = O(M · N)

Derivation Progression

State Definition

dp[i][j] = LCS(text_1[:i], text_2[:j])

(M+1) x (N+1) discrete alignment subproblems.

Character Match Invariant

if t1[i-1] == t2[j-1]: 1 + dp[i-1][j-1] else max(top, left)

Constant-time transition arithmetic.

Space Rolling

O(min(M, N)) row storage

Only previous row is needed for current row calculations.

Variable Definitions

MMM

Length of first string text1

NNN

Length of second string text2

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(min(M, N)) Two 1D arrays of size min(M, N)

🟢 Output Space

O(1) LCS length integer

Boundary Best / Worst Cases

Best Case

O(M⋅N)O(M · N)O(M⋅N)

Average Case

O(M⋅N)O(M · N)O(M⋅N)

Worst Case

O(M⋅N)O(M · N)O(M⋅N)

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: "longest common subsequence of two strings". 2D prefix string alignment: - If s1[i−1]==s2[j−1]s_1[i-1] == s_2[j-1]s1​[i−1]==s2​[j−1]: match diagonal 1+dp[i−1][j−1]1 + dp[i-1][j-1]1+dp[i−1][j−1]. - Else: take max of deleting from s1s_1s1​ (dp[i−1][j]dp[i-1][j]dp[i−1][j]) or s2s_2s2​ (dp[i][j−1]dp[i][j-1]dp[i][j−1]).

CONSTRAINTS & BOUNDS

M,N≤1000  ⟹  MN≤106M, N \le 1000 \implies MN \le 10^6M,N≤1000⟹MN≤106. Time budget: O(MN)O(MN)O(MN) operations (<150< 150<150ms).

FAANG PRODUCTION TRAPS & EDGE CASES

1-indexed DP coordinates avoid negative index checks on empty string base cases.

Theory Context•Dynamic Programming
MediumLC 1143

Longest Common Subsequence (LeetCode 1143)

Target Frequency:AmazonMetaNetflix

You're handed two strings, text1 and text2. A subsequence of a string is whatever remains after deleting zero or more of its characters while keeping the survivors in their original order -- for instance, "ace" can be pulled out of "abcde" this way. A common subsequence is a subsequence the two strings share.

Report the length of the longest subsequence they have in common, or 0 if they share none.

Worked Examples

Example 1
Input:text1 = "abcde", text2 = "ace"
Output:3
Explanation: Both strings share the subsequence "ace", giving a length of 3.
Example 2
Input:text1 = "abc", text2 = "abc"
Output:3
Explanation: Since the two strings are identical, their longest shared subsequence is the whole string "abc", length 3.
Example 3
Input:text1 = "abc", text2 = "def"
Output:0
Explanation: The two strings share no characters at all, so the answer is 0.

⚖️Formal Constraints & Bounds

  • 1 <= text1.length, text2.length <= 1000

  • text1 and text2 consist of only lowercase English characters.

Deep-Dive & Conceptual Insights

Core Insight

Defining the subproblem on string prefixes turns full comparison into a 2-D recurrence: match extends diagonal by 1; mismatch takes the maximum of dropping the current character from either string.

Real-World Scenario & Production Applications

Git diff algorithm (Myers diff), DNA sequence alignment in computational genomics (BLAST/Smith-Waterman), and fuzzy text similarity.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: 2D Subproblem Matrix Setup

Define dp[i][j] as the length of the Longest Common Subsequence between prefix text1[:i] and prefix text2[:j]. Empty prefixes initialize to 0 (dp[0][j] = 0, dp[i][0] = 0).

Mathematical Recurrence / Code Invariant
# dp[i][j] = LCS length of text1[:i] and text2[:j]
dp = [[0] * (N + 1) for _ in range(M + 1)]

Step-by-Step Execution Trace Table

dp[i][j]""ace
""0000
a01 (diagonal match)11
b0111
c012 (diagonal match)2
d0122
e0123 (diagonal match →\to→ Final Answer)
Code Shape
Archetype Code Shape
Python
1dp = [[0] * (n + 1) for _ in range(m + 1)]
2for i in range(1, m + 1):
3 for j in range(1, n + 1):
4 if text1[i-1] == text2[j-1]:
5 dp[i][j] = dp[i-1][j-1] + 1
6 else:
7 dp[i][j] = max(dp[i-1][j], dp[i][j-1])
8return dp[m][n]

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition
Code / Blueprint
Longest Common Subsequence aligns two strings $s_1$ and $s_2$. At cell $(i, j)$ of the 2D DP matrix, if characters match (`s_1[i-1] == s_2[j-1]`), the alignment extends diagonally: $dp[i][j] = dp[i-1][j-1] + 1$. If they differ, the optimal alignment discards one character from either string: $dp[i][j] = max(dp[i-1][j], dp[i][j-1])$. This evaluates all prefix subproblems in $O(M \times N)$ time and $O(min(M, N))$ space.
 
🧩 The Analogy: Aligning Two Rows of Puzzle Pieces

Imagine laying two strings out as two rows of labeled tiles, and you're building an alignment table cell by cell. At cell (i, j), you're asking: "what's the longest common subsequence between the first i letters of text1 and the first j letters of text2?" If the two letters at this position match, you get to extend the alignment diagonally — the answer here is 1 more than whatever the alignment was before both letters were added (dp[i-1][j-1]). If they don't match, you can't extend a match here at all, so you inherit the best alignment achievable by dropping just one letter from either string — whichever of dp[i-1][j] or dp[i][j-1] is bigger.

🔑 The Key Idea in the Code

A match always moves diagonally (both strings advance together); a mismatch always moves orthogonally (only one string advances) — this is why LCS's DP table is famously visualized with diagonal arrows tracing the matched characters.

  • 1-based DP table coordinate shift off-by-one: Forgetting that table cell dp[i][j] maps to string indices text1[i - 1] and text2[j - 1]. Comparing text1[i] == text2[j] causes string out-of-bounds or misaligned character comparisons.

  • Mismatch transition cost: Adding +1+1+1 on mismatch states: 1 + max(dp[i-1][j], dp[i][j-1]). Mismatch transitions carry no reward; the recurrence is strictly max(dp[i-1][j], dp[i][j-1]).

  • Diagonal overwrite in 1D space optimization: In rolling 1D space, updating dp[j] overwrites the value from the previous row that dp[j+1] needs as its top-left diagonal neighbor dp[i-1][j-1]. A temporary variable prev must store the diagonal value before overwriting.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Subsequence alignment between two strings + ordering preserved + finding longest shared elements.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Recurrence: if s1[i-1] == s2[j-1]: dp[i][j] = 1 + dp[i-1][j-1]; else dp[i][j] = max(dp[i-1][j], dp[i][j-1]).

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Off-by-one indexing: grid dimensions must be (M+1) x (N+1) so row 0 and col 0 represent empty string prefixes.

  • Allocating 2D grid with shallow copies (e.g. [[0]*(N+1)]*(M+1) in Python) which causes mutated rows.

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I use a 2D DP table of size (M+1) x (N+1). When characters match, I extend the optimal prefix alignment from dp[i-1][j-1] by 1. On mismatch, I take the maximum of dropping a character from text1 or text2. This yields an optimal O(M * N) time and space solution."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(M · N)

Look at the execution steps: 2D DP table of size M x N where text1 has length M and text2 has length N. Transition: if characters match, 1 + diag; else max(top, left). Rolling rows reduces space to O(min(M, N)).

SPACE COMPLEXITY

O(min(M, N)) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Two rows of size min(M, N) + 1.

Formal Recurrence Relation

T(M, N) = M · N = O(M · N)

Derivation Progression

State Definition

dp[i][j] = LCS(text_1[:i], text_2[:j])

(M+1) x (N+1) discrete alignment subproblems.

Character Match Invariant

if t1[i-1] == t2[j-1]: 1 + dp[i-1][j-1] else max(top, left)

Constant-time transition arithmetic.

Space Rolling

O(min(M, N)) row storage

Only previous row is needed for current row calculations.

Variable Definitions

MMM

Length of first string text1

NNN

Length of second string text2

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(min(M, N)) Two 1D arrays of size min(M, N)

🟢 Output Space

O(1) LCS length integer

Boundary Best / Worst Cases

Best Case

O(M⋅N)O(M · N)O(M⋅N)

Average Case

O(M⋅N)O(M · N)O(M⋅N)

Worst Case

O(M⋅N)O(M · N)O(M⋅N)

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: "longest common subsequence of two strings". 2D prefix string alignment: - If s1[i−1]==s2[j−1]s_1[i-1] == s_2[j-1]s1​[i−1]==s2​[j−1]: match diagonal 1+dp[i−1][j−1]1 + dp[i-1][j-1]1+dp[i−1][j−1]. - Else: take max of deleting from s1s_1s1​ (dp[i−1][j]dp[i-1][j]dp[i−1][j]) or s2s_2s2​ (dp[i][j−1]dp[i][j-1]dp[i][j−1]).

CONSTRAINTS & BOUNDS

M,N≤1000  ⟹  MN≤106M, N \le 1000 \implies MN \le 10^6M,N≤1000⟹MN≤106. Time budget: O(MN)O(MN)O(MN) operations (<150< 150<150ms).

FAANG PRODUCTION TRAPS & EDGE CASES

1-indexed DP coordinates avoid negative index checks on empty string base cases.

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