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•Binary Search Boundary
MediumLC 74

Search a 2D Matrix (LeetCode 74)

Target Frequency:AmazonMicrosoftBloomberg

You're given an m x n grid of integers, matrix, with two guarantees: each row is sorted left-to-right (non-decreasing), and every row's first value is bigger than the previous row's last value — so reading the grid row by row, the numbers only ever increase.

Given a target value, decide whether it appears anywhere in matrix.

Your solution must run in O(log(m * n)) time.

Worked Examples

Example 1
Input:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output:true
13571011162023303460
Explanation: Read row by row, the grid is 1, 3, 5, 7, 10, 11, 16, 20, 23, 30, 34, 60; 3 is there, at matrix[0][1].
Example 2
Input:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output:false
13571011162023303460
Explanation: 13 would fall between 11 and 16 in the second row, and it is not in the grid.

⚖️Formal Constraints & Bounds

  • m == matrix.length

  • n == matrix[i].length

  • 1 <= m, n <= 100

  • -104 <= matrix[i][j], target <= 104

Deep-Dive & Conceptual Insights

Core Insight

Reading row by row gives one sorted list, and position mid of that list is matrix[mid // n][mid % n]: binary search the positions and never build the list.

Real-World Scenario & Production Applications

Fixed-size pages: a sorted log split into pages of n records stores record k on page k // n at slot k % n. Looking a key up means binary searching record numbers and reading only the pages the probes land on, never loading the whole log into one array.

Step-by-Step Execution Trace Table

Step[lo, hi]midrow, col = divmod(mid, 4)valCompare with 3Action
1[0, 11]5(1, 1)1111 > 3hi = mid - 1 = 4
2[0, 4]2(0, 2)55 > 3hi = mid - 1 = 1
3[0, 1]0(0, 0)11 < 3lo = mid + 1 = 1
4[1, 1]1(0, 1)33 == 3Return True
Code Shape
Archetype Code Shape
Python
1Map i to matrix[i // n][i % n].
2Use Lower Bound over [0, m * n).
3Guard the returned index before checking equality.

Conceptual Narrative

The first value of each row is larger than the last value of the row before it, so reading the matrix row by row gives one sorted list of m * n values. Do not build that list: flat index i names the cell matrix[i // n][i % n], so pass at_least_target and the range [0, m * n) to binary_search_lower_bound, then check the returned cell for equality. Each row holds n cells, so i // n full rows come before index i, and the cell is i % n places into its own row. The cells never decrease as i grows, so at_least_target is False and then True. If target is in the matrix, the first True cell holds it. If not, that cell holds a larger value, or binary_search_lower_bound returns m * n, which is one past the last cell. The trap is the divisor: use n, the number of columns, not m. A square matrix has m == n and hides the wrong divisor, so the bug appears only on another shape.

  • Dividing the flat index by the number of rows

  • Reading the index m * n before checking the range

  • Using equality as the check

  • Writing > instead of >= in the check

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Each row starts above the preceding row's last value. Reading row by row gives one sorted list, so the check matrix[i // n][i % n] >= target is monotonic.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Lower Bound searches [0, m * n) for the first cell at least target. Index i maps to row i // n and column i % n. After the search, i < m * n guards the equality check.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Divide by n, the number of columns. A square matrix hides a wrong divisor.

  • Guard i < m * n before reading the final cell.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I would read the matrix as one globally sorted row-major list without copying it. Each row has n cells, so flat index i maps to matrix[i // n][i % n]. I pass the monotonic at-least-target check and [0, m * n) to Lower Bound. Then I check the returned index is inside the matrix before testing equality. This takes O(log(m * n)) time and O(1) extra space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(log(M * N))

Look at the code: lo = 0; hi = m * n starts with all M * N flat positions. Each pass of while lo < hi does one index mapping, one cell read and one comparison, then keeps the first passing candidate with hi = mid, or discards failed candidates with lo = mid + 1. The range shrinks M·N → M·N/2 → ... → 1, so there are at most floor(log2(M·N)) + 1 passes of constant work: O(log(M * N)).

SPACE COMPLEXITY

O(1)

The code keeps m, n, lo, hi, mid, row, col and val, whatever the matrix size, and returns one boolean. It never builds the flat list, so the extra space is O(1).

Formal Recurrence Relation

T(M·N) = T(M·N / 2) + O(1) = O(log(M * N))

Derivation Progression

Setup

O(1)

m, n = len(matrix), len(matrix[0]) and lo = 0; hi = m * n are constant work.

Halving loop

at most floor(log2(M·N)) + 1 passes

Each pass of while lo < hi narrows the candidate range with hi = mid or lo = mid + 1.

One probe

O(1) per pass

matrix[i // n][i % n], one cell read and one comparison, whatever M and N are.

Total

O(log(M * N))

Constant work times a logarithmic number of passes.

Variable Definitions

MMM

Number of rows, len(matrix)

NNN

Number of columns, len(matrix[0])

midmidmid

The flat index being probed, a position in the row-by-row list

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative, no recursion

🔵 Auxiliary Heap

O(1): m, n, lo, hi, mid, row, col, val; no flattened copy

🟢 Output Space

O(1): one boolean

Boundary Best / Worst Cases

Best Case

O(log⁡(M⋅N))O(\log(M \cdot N))O(log(M⋅N)): the template searches for the boundary before the equality check

Average Case

O(log⁡(M⋅N))O(\log(M \cdot N))O(log(M⋅N))

Worst Case

O(log⁡(M⋅N))O(\log(M \cdot N))O(log(M⋅N)): target is absent or found on the last probe

Recurrence Tree Topology

Recurrence Tree Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "every row's first value is bigger than the previous row's last value" and "must run in O(log(m * n)) time". The grid is one sorted list stored row by row, so the move is Global Matrix Bisection: Lower Bound on the flat index, using matrix[i // n][i % n] >= target, then one guarded equality check.

CONSTRAINTS & BOUNDS

m,n≤100m, n \le 100m,n≤100, so at most 10410^4104 cells and about 14 probes. Budget: O(log⁡(m⋅n))O(\log(m \cdot n))O(log(m⋅n)) time, O(1)O(1)O(1) space. For grids with more than 2312^{31}231 cells the flat index needs a 64-bit integer in Java, C# or C++.

FAANG PRODUCTION TRAPS & EDGE CASES

Copying the grid into one list first costs O(m⋅n)O(m \cdot n)O(m⋅n) time and memory and throws the log bound away. When each row is a separate page or block in storage, every probe may be a separate read, so the probe count, about log⁡2(m⋅n)\log_2(m \cdot n)log2​(m⋅n), is what the design must keep small; fetch only the probed rows.

Theory Context•Binary Search Boundary
MediumLC 74

Search a 2D Matrix (LeetCode 74)

Target Frequency:AmazonMicrosoftBloomberg

You're given an m x n grid of integers, matrix, with two guarantees: each row is sorted left-to-right (non-decreasing), and every row's first value is bigger than the previous row's last value — so reading the grid row by row, the numbers only ever increase.

Given a target value, decide whether it appears anywhere in matrix.

Your solution must run in O(log(m * n)) time.

Worked Examples

Example 1
Input:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output:true
13571011162023303460
Explanation: Read row by row, the grid is 1, 3, 5, 7, 10, 11, 16, 20, 23, 30, 34, 60; 3 is there, at matrix[0][1].
Example 2
Input:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output:false
13571011162023303460
Explanation: 13 would fall between 11 and 16 in the second row, and it is not in the grid.

⚖️Formal Constraints & Bounds

  • m == matrix.length

  • n == matrix[i].length

  • 1 <= m, n <= 100

  • -104 <= matrix[i][j], target <= 104

Deep-Dive & Conceptual Insights

Core Insight

Reading row by row gives one sorted list, and position mid of that list is matrix[mid // n][mid % n]: binary search the positions and never build the list.

Real-World Scenario & Production Applications

Fixed-size pages: a sorted log split into pages of n records stores record k on page k // n at slot k % n. Looking a key up means binary searching record numbers and reading only the pages the probes land on, never loading the whole log into one array.

Step-by-Step Execution Trace Table

Step[lo, hi]midrow, col = divmod(mid, 4)valCompare with 3Action
1[0, 11]5(1, 1)1111 > 3hi = mid - 1 = 4
2[0, 4]2(0, 2)55 > 3hi = mid - 1 = 1
3[0, 1]0(0, 0)11 < 3lo = mid + 1 = 1
4[1, 1]1(0, 1)33 == 3Return True
Code Shape
Archetype Code Shape
Python
1Map i to matrix[i // n][i % n].
2Use Lower Bound over [0, m * n).
3Guard the returned index before checking equality.

Conceptual Narrative

The first value of each row is larger than the last value of the row before it, so reading the matrix row by row gives one sorted list of m * n values. Do not build that list: flat index i names the cell matrix[i // n][i % n], so pass at_least_target and the range [0, m * n) to binary_search_lower_bound, then check the returned cell for equality. Each row holds n cells, so i // n full rows come before index i, and the cell is i % n places into its own row. The cells never decrease as i grows, so at_least_target is False and then True. If target is in the matrix, the first True cell holds it. If not, that cell holds a larger value, or binary_search_lower_bound returns m * n, which is one past the last cell. The trap is the divisor: use n, the number of columns, not m. A square matrix has m == n and hides the wrong divisor, so the bug appears only on another shape.

  • Dividing the flat index by the number of rows

  • Reading the index m * n before checking the range

  • Using equality as the check

  • Writing > instead of >= in the check

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Each row starts above the preceding row's last value. Reading row by row gives one sorted list, so the check matrix[i // n][i % n] >= target is monotonic.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Lower Bound searches [0, m * n) for the first cell at least target. Index i maps to row i // n and column i % n. After the search, i < m * n guards the equality check.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Divide by n, the number of columns. A square matrix hides a wrong divisor.

  • Guard i < m * n before reading the final cell.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I would read the matrix as one globally sorted row-major list without copying it. Each row has n cells, so flat index i maps to matrix[i // n][i % n]. I pass the monotonic at-least-target check and [0, m * n) to Lower Bound. Then I check the returned index is inside the matrix before testing equality. This takes O(log(m * n)) time and O(1) extra space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(log(M * N))

Look at the code: lo = 0; hi = m * n starts with all M * N flat positions. Each pass of while lo < hi does one index mapping, one cell read and one comparison, then keeps the first passing candidate with hi = mid, or discards failed candidates with lo = mid + 1. The range shrinks M·N → M·N/2 → ... → 1, so there are at most floor(log2(M·N)) + 1 passes of constant work: O(log(M * N)).

SPACE COMPLEXITY

O(1)

The code keeps m, n, lo, hi, mid, row, col and val, whatever the matrix size, and returns one boolean. It never builds the flat list, so the extra space is O(1).

Formal Recurrence Relation

T(M·N) = T(M·N / 2) + O(1) = O(log(M * N))

Derivation Progression

Setup

O(1)

m, n = len(matrix), len(matrix[0]) and lo = 0; hi = m * n are constant work.

Halving loop

at most floor(log2(M·N)) + 1 passes

Each pass of while lo < hi narrows the candidate range with hi = mid or lo = mid + 1.

One probe

O(1) per pass

matrix[i // n][i % n], one cell read and one comparison, whatever M and N are.

Total

O(log(M * N))

Constant work times a logarithmic number of passes.

Variable Definitions

MMM

Number of rows, len(matrix)

NNN

Number of columns, len(matrix[0])

midmidmid

The flat index being probed, a position in the row-by-row list

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative, no recursion

🔵 Auxiliary Heap

O(1): m, n, lo, hi, mid, row, col, val; no flattened copy

🟢 Output Space

O(1): one boolean

Boundary Best / Worst Cases

Best Case

O(log⁡(M⋅N))O(\log(M \cdot N))O(log(M⋅N)): the template searches for the boundary before the equality check

Average Case

O(log⁡(M⋅N))O(\log(M \cdot N))O(log(M⋅N))

Worst Case

O(log⁡(M⋅N))O(\log(M \cdot N))O(log(M⋅N)): target is absent or found on the last probe

Recurrence Tree Topology

Recurrence Tree Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "every row's first value is bigger than the previous row's last value" and "must run in O(log(m * n)) time". The grid is one sorted list stored row by row, so the move is Global Matrix Bisection: Lower Bound on the flat index, using matrix[i // n][i % n] >= target, then one guarded equality check.

CONSTRAINTS & BOUNDS

m,n≤100m, n \le 100m,n≤100, so at most 10410^4104 cells and about 14 probes. Budget: O(log⁡(m⋅n))O(\log(m \cdot n))O(log(m⋅n)) time, O(1)O(1)O(1) space. For grids with more than 2312^{31}231 cells the flat index needs a 64-bit integer in Java, C# or C++.

FAANG PRODUCTION TRAPS & EDGE CASES

Copying the grid into one list first costs O(m⋅n)O(m \cdot n)O(m⋅n) time and memory and throws the log bound away. When each row is a separate page or block in storage, every probe may be a separate read, so the probe count, about log⁡2(m⋅n)\log_2(m \cdot n)log2​(m⋅n), is what the design must keep small; fetch only the probed rows.

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