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 152

Maximum Product Subarray (LeetCode 152)

Target Frequency:AmazonGoogleMicrosoft

You get an integer array nums. A subarray is a run of numbers that sit next to each other. Multiply the numbers of a subarray to get its product.

Find the subarray with the largest product and return that product. The array can hold negative numbers and zeros. The subarray must hold at least one number.

Worked Examples

Example 1
Input:nums = [2,3,-2,4]
Output:6
Explanation: The subarray [2, 3] has product 6. No other subarray is larger: -2 makes any run that holds it smaller, and [4] is only 4.
Example 2
Input:nums = [-2,0,-1]
Output:0
Explanation: -2 and -1 have product 2, but they are not next to each other, so they are not a subarray. The best choice is [0], with product 0.

⚖️Formal Constraints & Bounds

  • 1 <= nums.length <= 2 * 104

  • -10 <= nums[i] <= 10

  • The product of any subarray of nums fits in a 32-bit integer.

Deep-Dive & Conceptual Insights

Core Insight

Multiplying by a negative number turns the smallest product into the largest one, so the best subarray ending at x comes from x alone, from the largest product extended by x, or from the smallest product extended by x: keep both ends for every position.

Real-World Scenario & Production Applications

Growth rates that multiply, such as a daily change of 1.02 or 0.97 in a price: the best stretch of days is the run with the largest product of factors, and one bad factor can be undone by another.

Step-by-Step Execution Trace Table

Trace of Example 1: nums = [2, 3, -2, 4]. hi, lo and best start at nums[0] = 2.

xCandidates x, hi * x, lo * xhilobest
33, 6, 6636
-2-2, -12, -6-2-126
44, -8, -484-486

At x = -2, the largest product ending here is -2 alone and the smallest is -12. A later negative number would turn -12 into the largest, which is why lo is kept.

Code Shape
Archetype Code Shape
Python
1hi = lo = best = nums[0]
2for x in nums[1:]:
3 hi, lo = max(x, hi * x, lo * x), min(x, hi * x, lo * x)
4 best = max(best, hi)
5return best

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Kadane's Algorithm keeps one value per position: the best sum that ends here. For a product, one value is not enough. Multiplying by a negative number turns the smallest product into the largest one. So this variant keeps two values per position: hi, the largest product of a subarray that ends here, and lo, the smallest. Each new number x gives three candidates: x alone, hi * x and lo * x.

➗ What Is Added To Kadane

Kadane's Algorithm chooses between extending the run and starting fresh. This variant keeps that choice and adds one more piece of state, lo, because the worst product so far can become the best one after the next negative number.

🔑 The Key Idea in the Code

hi and lo are both computed from the old hi and the old lo, in one assignment. If hi is updated first, lo would be computed from the new hi, which is a different subarray.

  • Keeping only the largest product: with curr = max(x, curr * x), a negative x multiplies the largest product into a smaller one, and the smallest product that could turn positive is lost. [-2, 3, -4] returns 3 instead of 24.

  • Updating hi before computing lo: after hi changes, hi * x is a different subarray. Compute both in one assignment from the old values.

  • Leaving x out of the candidates: after a zero, hi and lo are both 0 and only x alone can restart the run. [0, 2] returns 0 instead of 2.

  • Starting best at 0: the subarray must hold at least one number, so an array of one negative number has a negative answer.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The largest product of a subarray": a best value over contiguous runs, like Kadane's Algorithm, but the operation is multiplication and the numbers may be negative. A negative number swaps the smallest and the largest, which tells you one running value is not enough.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

hi is the largest and lo is the smallest product of a subarray that ends at x. The next pair comes from three candidates: x alone, hi * x and lo * x. best keeps the largest hi.

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 Kadane's Algorithm and one extra piece of state. The best product ending at a number is not enough, because a negative number turns the smallest product into the largest. So I keep hi and lo, the largest and smallest product of a subarray ending at the current number. For each number x, the new hi is the maximum of x, hi times x and lo times x, and the new lo is the minimum of the same three values, both computed from the old hi and lo. Zeros restart the run automatically, because x alone is always a candidate. I keep the largest hi in best. That is one pass, order N time and order 1 space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the loop. for x in nums[1:] visits each number once. Each visit computes three products, one max, one min and one comparison, which is constant work. That is N steps, so time is O(N).

SPACE COMPLEXITY

O(1)

The code keeps three variables, hi, lo and best, whatever the length of nums is. There is no table and no recursion, so space is O(1).

Formal Recurrence Relation

T(N)=3N=O(N)T(N) = 3N = O(N)T(N)=3N=O(N)

Derivation Progression

Passes

N−1 numbersN - 1 \text{ numbers}N−1 numbers

for x in nums[1:] visits each number after the first once.

Work per number

3 = O(1)

Three products, one max, one min and one comparison.

Total

3N = O(N)

Numbers times a constant amount of work.

Variable Definitions

NNN

Length of nums

hihihi

Largest product of a subarray that ends at the current number

lololo

Smallest product of a subarray that ends at the current number

Memory Architecture & Bounds

🟣 Call Stack

O(1): a loop only, no recursion

🔵 Auxiliary Heap

O(1): the variables hi, lo and best

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N): the loop always visits every number.

Average Case

O(N)O(N)O(N).

Worst Case

O(N)O(N)O(N): 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 largest product of a subarray", "negative numbers". It is a best value over contiguous runs, so this is Kadane's Algorithm, and the negative numbers mean it needs the smallest product as well.

CONSTRAINTS & BOUNDS

N≤2×104N \le 2 \times 10^4N≤2×104 and ∣nums[i]∣≤10|nums[i]| \le 10∣nums[i]∣≤10. A loop over all pairs is 2×1082 \times 10^82×108 products; one pass is O(N)O(N)O(N). The problem guarantees every product fits in 32 bits, so no overflow handling is needed here.

FAANG PRODUCTION TRAPS & EDGE CASES

In a language with fixed-width integers, a product of many large numbers can overflow, which turns the smallest product into a large positive one. LeetCode guarantees the answer fits in 32 bits, but intermediate lo values of a run that is later discarded could be larger than that bound, so some solutions use a 64-bit type.

Theory Context•Dynamic Programming
MediumLC 152

Maximum Product Subarray (LeetCode 152)

Target Frequency:AmazonGoogleMicrosoft

You get an integer array nums. A subarray is a run of numbers that sit next to each other. Multiply the numbers of a subarray to get its product.

Find the subarray with the largest product and return that product. The array can hold negative numbers and zeros. The subarray must hold at least one number.

Worked Examples

Example 1
Input:nums = [2,3,-2,4]
Output:6
Explanation: The subarray [2, 3] has product 6. No other subarray is larger: -2 makes any run that holds it smaller, and [4] is only 4.
Example 2
Input:nums = [-2,0,-1]
Output:0
Explanation: -2 and -1 have product 2, but they are not next to each other, so they are not a subarray. The best choice is [0], with product 0.

⚖️Formal Constraints & Bounds

  • 1 <= nums.length <= 2 * 104

  • -10 <= nums[i] <= 10

  • The product of any subarray of nums fits in a 32-bit integer.

Deep-Dive & Conceptual Insights

Core Insight

Multiplying by a negative number turns the smallest product into the largest one, so the best subarray ending at x comes from x alone, from the largest product extended by x, or from the smallest product extended by x: keep both ends for every position.

Real-World Scenario & Production Applications

Growth rates that multiply, such as a daily change of 1.02 or 0.97 in a price: the best stretch of days is the run with the largest product of factors, and one bad factor can be undone by another.

Step-by-Step Execution Trace Table

Trace of Example 1: nums = [2, 3, -2, 4]. hi, lo and best start at nums[0] = 2.

xCandidates x, hi * x, lo * xhilobest
33, 6, 6636
-2-2, -12, -6-2-126
44, -8, -484-486

At x = -2, the largest product ending here is -2 alone and the smallest is -12. A later negative number would turn -12 into the largest, which is why lo is kept.

Code Shape
Archetype Code Shape
Python
1hi = lo = best = nums[0]
2for x in nums[1:]:
3 hi, lo = max(x, hi * x, lo * x), min(x, hi * x, lo * x)
4 best = max(best, hi)
5return best

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Kadane's Algorithm keeps one value per position: the best sum that ends here. For a product, one value is not enough. Multiplying by a negative number turns the smallest product into the largest one. So this variant keeps two values per position: hi, the largest product of a subarray that ends here, and lo, the smallest. Each new number x gives three candidates: x alone, hi * x and lo * x.

➗ What Is Added To Kadane

Kadane's Algorithm chooses between extending the run and starting fresh. This variant keeps that choice and adds one more piece of state, lo, because the worst product so far can become the best one after the next negative number.

🔑 The Key Idea in the Code

hi and lo are both computed from the old hi and the old lo, in one assignment. If hi is updated first, lo would be computed from the new hi, which is a different subarray.

  • Keeping only the largest product: with curr = max(x, curr * x), a negative x multiplies the largest product into a smaller one, and the smallest product that could turn positive is lost. [-2, 3, -4] returns 3 instead of 24.

  • Updating hi before computing lo: after hi changes, hi * x is a different subarray. Compute both in one assignment from the old values.

  • Leaving x out of the candidates: after a zero, hi and lo are both 0 and only x alone can restart the run. [0, 2] returns 0 instead of 2.

  • Starting best at 0: the subarray must hold at least one number, so an array of one negative number has a negative answer.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"The largest product of a subarray": a best value over contiguous runs, like Kadane's Algorithm, but the operation is multiplication and the numbers may be negative. A negative number swaps the smallest and the largest, which tells you one running value is not enough.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

hi is the largest and lo is the smallest product of a subarray that ends at x. The next pair comes from three candidates: x alone, hi * x and lo * x. best keeps the largest hi.

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 Kadane's Algorithm and one extra piece of state. The best product ending at a number is not enough, because a negative number turns the smallest product into the largest. So I keep hi and lo, the largest and smallest product of a subarray ending at the current number. For each number x, the new hi is the maximum of x, hi times x and lo times x, and the new lo is the minimum of the same three values, both computed from the old hi and lo. Zeros restart the run automatically, because x alone is always a candidate. I keep the largest hi in best. That is one pass, order N time and order 1 space.

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the loop. for x in nums[1:] visits each number once. Each visit computes three products, one max, one min and one comparison, which is constant work. That is N steps, so time is O(N).

SPACE COMPLEXITY

O(1)

The code keeps three variables, hi, lo and best, whatever the length of nums is. There is no table and no recursion, so space is O(1).

Formal Recurrence Relation

T(N)=3N=O(N)T(N) = 3N = O(N)T(N)=3N=O(N)

Derivation Progression

Passes

N−1 numbersN - 1 \text{ numbers}N−1 numbers

for x in nums[1:] visits each number after the first once.

Work per number

3 = O(1)

Three products, one max, one min and one comparison.

Total

3N = O(N)

Numbers times a constant amount of work.

Variable Definitions

NNN

Length of nums

hihihi

Largest product of a subarray that ends at the current number

lololo

Smallest product of a subarray that ends at the current number

Memory Architecture & Bounds

🟣 Call Stack

O(1): a loop only, no recursion

🔵 Auxiliary Heap

O(1): the variables hi, lo and best

🟢 Output Space

O(1): one integer

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N): the loop always visits every number.

Average Case

O(N)O(N)O(N).

Worst Case

O(N)O(N)O(N): 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 largest product of a subarray", "negative numbers". It is a best value over contiguous runs, so this is Kadane's Algorithm, and the negative numbers mean it needs the smallest product as well.

CONSTRAINTS & BOUNDS

N≤2×104N \le 2 \times 10^4N≤2×104 and ∣nums[i]∣≤10|nums[i]| \le 10∣nums[i]∣≤10. A loop over all pairs is 2×1082 \times 10^82×108 products; one pass is O(N)O(N)O(N). The problem guarantees every product fits in 32 bits, so no overflow handling is needed here.

FAANG PRODUCTION TRAPS & EDGE CASES

In a language with fixed-width integers, a product of many large numbers can overflow, which turns the smallest product into a large positive one. LeetCode guarantees the answer fits in 32 bits, but intermediate lo values of a run that is later discarded could be larger than that bound, so some solutions use a 64-bit type.

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