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•Two Pointers & Sliding Window
MediumLC 167

Two Sum II - Input Array Is Sorted (LeetCode 167)

Target Frequency:AmazonMetaGoogle

You're given numbers, a 1-indexed array of integers already sorted in non-decreasing order, plus a target value. Locate two distinct positions index_1 < index_2 whose values add up to target.

Return those two positions, 1-indexed, as [index_1, index_2].

Exactly one valid pair is guaranteed to exist, and you can't reuse the same element twice.

Do this using only constant extra space.

Worked Examples

Example 1
Input:numbers = [2,7,11,15], target = 9
Output:[1,2]
Explanation: 2 + 7 = 9, and those values sit at 1-indexed positions 1 and 2, so the answer is [1, 2].
Example 2
Input:numbers = [2,3,4], target = 6
Output:[1,3]
Explanation: 2 + 4 = 6, and those values sit at 1-indexed positions 1 and 3, so the answer is [1, 3].
Example 3
Input:numbers = [-1,0], target = -1
Output:[1,2]
Explanation: -1 + 0 = -1, and those values sit at 1-indexed positions 1 and 2, so the answer is [1, 2].

⚖️Formal Constraints & Bounds

  • 2 <= numbers.length <= 3 * 104

  • -1000 <= numbers[i] <= 1000

  • numbers is sorted in non-decreasing order.

  • -1000 <= target <= 1000

  • The tests are generated such that there is exactly one solution.

Deep-Dive & Conceptual Insights

Core Insight

Sortedness gives monotonicity: sum > target proves right element is too big for any valid pair; sum < target proves left element is too small.

Real-World Scenario & Production Applications

Financial portfolio pair matching and hardware bus packet alignment across memory banks.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Sorted-Array Boundary Initialization

Because the input is already sorted, start pointers at both extremes: left = 0, right = len(numbers) - 1.

Mathematical Recurrence / Code Invariant
left, right = 0, len(numbers) - 1

Step-by-Step Execution Trace Table

StepPointers (left, right)Current SumComparison vs target = 9Elimination DecisionNew (left, right)
1(0, 5)1 + 11 = 1212 > 9 (Too big)11 is too large for any partner →\to→ discard right end (right = 4)(0, 4)
2(0, 4)1 + 7 = 88 < 9 (Too small)1 is too small for any partner →\to→ discard left end (left = 1)(1, 4)
3(1, 4)3 + 7 = 1010 > 9 (Too big)Discard right end (right = 3)(1, 3)
4(1, 3)3 + 5 = 88 < 9 (Too small)Discard left end (left = 2)(2, 3)
5(2, 3)4 + 5 = 99 == 9 (Exact Match)Target reached! Return 1-indexed [2+1, 3+1] →\to→ [3, 4]—
Code Shape
Archetype Code Shape
Python
1left, right = 0, len(numbers) - 1
2while left < right:
3 curr_sum = numbers[left] + numbers[right]
4 if curr_sum == target:
5 return [left + 1, right + 1]
6 elif curr_sum < target:
7 left += 1
8 else:
9 right -= 1
10return []

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Discarding an entire endpoint after a single comparison feels risky — how do you know you're not throwing away the answer? Sortedness is what makes it provably safe.

⚖️ The Analogy: A Seesaw You Can Only Push From the Ends

Picture the smallest and largest numbers sitting on opposite ends of a seesaw. If their combined sum is too big, the only way to bring it down is to swap out the larger end for something smaller — so you move the right pointer inward. If the sum is too small, the only way to raise it is to swap out the smaller end for something bigger — so you move the left pointer inward. Because the array is sorted, moving a pointer inward is the only direction that can fix an over/under-shoot; moving it the other way would only make the imbalance worse.

🔑 The Key Idea in the Code

Every element you discard when you move a pointer is provably eliminated: if numbers[left] + numbers[right] > target, then numbers[left] + numbers[anything smaller than right] is also too big, since the array is sorted — so skipping every intermediate right value is completely safe.

  • 1-based indexing on the return value: LC 167 requires the 1-indexed positions [left + 1, right + 1], not the raw 0-indexed left/right; returning 0-indexed positions passes every trace by eye but fails the judge.

  • Moving both pointers after a mismatch: only the pointer the comparison rules out may move; moving both skips pairs, so numbers = [1, 2, 4, 6], target = 8 never checks 2 + 6.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Sorted array + find the pair that adds up to target, answered as 1-indexed positions, with only O(1) extra space: squeeze from both ends (Converging Pointers).

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: every pair that uses an index outside [left, right] cannot sum to target; one comparison of numbers[left] + numbers[right] with target rules out one more index.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Unsorted input: moving a pointer no longer raises or lowers the sum, so the squeeze can walk past the answer; the array must be sorted (LC 167 promises it).

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I'd use Converging Pointers: left starts at index 0 and right at the last index. Because numbers is sorted, if the sum is smaller than target, numbers[left] is already paired with the largest value still available, so it can never reach target and I move left forward. If the sum is too big, numbers[right] is already paired with the smallest value still available, so I move right back. Each step rules out one element for good, which is why the pair can never be skipped. The trap is input that is not sorted: then moving a pointer no longer raises or lowers the sum, so the squeeze can walk past the answer. For unsorted input I would use a hash map, or sort value and index pairs to keep the original positions. Here the input is sorted and 1-indexed, so I return left + 1 and right + 1. It's O(N) time and O(1) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the while loop: while left < right:. In each iteration, we evaluate current_sum = numbers[left] + numbers[right]. If the sum is too small, we increment left += 1; if too large, we decrement right -= 1. Because the distance (right - left) strictly decreases by 1 on every step, the loop executes at most N-1 times with O(1) arithmetic per step, yielding strictly O(N) time.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocation: The search is iterative with O(1) call stack frames. We only store two scalar pointer variables (left, right) and one scalar sum accumulator. Auxiliary space is strictly O(1) in-place.

Formal Recurrence Relation

∑ (delta L + delta R) ≤ N ⟹ O(N)

Derivation Progression

Search Interval Monotonicity

(right - left) decreases by 1 each step

Sorted order guarantees that adjusting left rightward increases the sum and adjusting right leftward decreases the sum.

O(1) Arithmetic Per Step

O(1) sum check

Testing current_sum == target takes constant time with zero inner loops.

Linear Bounded Termination

N - 1 maximum steps ⟹ O(N)

The pointers converge from opposite ends, inspecting at most N elements before finding the pair.

Variable Definitions

NNN

Total number of elements in the sorted array

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative loop with zero recursion frames

🔵 Auxiliary Heap

O(1) Only scalar pointers (left, right) in CPU registers

🟢 Output Space

O(1) Returns 2-element index list

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1) if outer pair sums to target.

Average Case

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

Worst Case

O(N)O(N)O(N) when pair is in the middle.

Pointer Invariant Transition Progression

Pointer Invariant Transition Progression
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "array is already sorted in non-decreasing order", "find two numbers that add up to target", "O(1) extra space". Converging opposite-direction pointers exploiting monotonic ordering.

CONSTRAINTS & BOUNDS

2≤N≤3×1042 \le N \le 3 \times 10^42≤N≤3×104. Time budget strictly O(N)O(N)O(N), space strictly O(1)O(1)O(1).

FAANG PRODUCTION TRAPS & EDGE CASES

Values stay within [−1000,1000][-1000, 1000][−1000,1000], so the sum cannot overflow here.

Theory Context•Two Pointers & Sliding Window
MediumLC 167

Two Sum II - Input Array Is Sorted (LeetCode 167)

Target Frequency:AmazonMetaGoogle

You're given numbers, a 1-indexed array of integers already sorted in non-decreasing order, plus a target value. Locate two distinct positions index_1 < index_2 whose values add up to target.

Return those two positions, 1-indexed, as [index_1, index_2].

Exactly one valid pair is guaranteed to exist, and you can't reuse the same element twice.

Do this using only constant extra space.

Worked Examples

Example 1
Input:numbers = [2,7,11,15], target = 9
Output:[1,2]
Explanation: 2 + 7 = 9, and those values sit at 1-indexed positions 1 and 2, so the answer is [1, 2].
Example 2
Input:numbers = [2,3,4], target = 6
Output:[1,3]
Explanation: 2 + 4 = 6, and those values sit at 1-indexed positions 1 and 3, so the answer is [1, 3].
Example 3
Input:numbers = [-1,0], target = -1
Output:[1,2]
Explanation: -1 + 0 = -1, and those values sit at 1-indexed positions 1 and 2, so the answer is [1, 2].

⚖️Formal Constraints & Bounds

  • 2 <= numbers.length <= 3 * 104

  • -1000 <= numbers[i] <= 1000

  • numbers is sorted in non-decreasing order.

  • -1000 <= target <= 1000

  • The tests are generated such that there is exactly one solution.

Deep-Dive & Conceptual Insights

Core Insight

Sortedness gives monotonicity: sum > target proves right element is too big for any valid pair; sum < target proves left element is too small.

Real-World Scenario & Production Applications

Financial portfolio pair matching and hardware bus packet alignment across memory banks.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Sorted-Array Boundary Initialization

Because the input is already sorted, start pointers at both extremes: left = 0, right = len(numbers) - 1.

Mathematical Recurrence / Code Invariant
left, right = 0, len(numbers) - 1

Step-by-Step Execution Trace Table

StepPointers (left, right)Current SumComparison vs target = 9Elimination DecisionNew (left, right)
1(0, 5)1 + 11 = 1212 > 9 (Too big)11 is too large for any partner →\to→ discard right end (right = 4)(0, 4)
2(0, 4)1 + 7 = 88 < 9 (Too small)1 is too small for any partner →\to→ discard left end (left = 1)(1, 4)
3(1, 4)3 + 7 = 1010 > 9 (Too big)Discard right end (right = 3)(1, 3)
4(1, 3)3 + 5 = 88 < 9 (Too small)Discard left end (left = 2)(2, 3)
5(2, 3)4 + 5 = 99 == 9 (Exact Match)Target reached! Return 1-indexed [2+1, 3+1] →\to→ [3, 4]—
Code Shape
Archetype Code Shape
Python
1left, right = 0, len(numbers) - 1
2while left < right:
3 curr_sum = numbers[left] + numbers[right]
4 if curr_sum == target:
5 return [left + 1, right + 1]
6 elif curr_sum < target:
7 left += 1
8 else:
9 right -= 1
10return []

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Discarding an entire endpoint after a single comparison feels risky — how do you know you're not throwing away the answer? Sortedness is what makes it provably safe.

⚖️ The Analogy: A Seesaw You Can Only Push From the Ends

Picture the smallest and largest numbers sitting on opposite ends of a seesaw. If their combined sum is too big, the only way to bring it down is to swap out the larger end for something smaller — so you move the right pointer inward. If the sum is too small, the only way to raise it is to swap out the smaller end for something bigger — so you move the left pointer inward. Because the array is sorted, moving a pointer inward is the only direction that can fix an over/under-shoot; moving it the other way would only make the imbalance worse.

🔑 The Key Idea in the Code

Every element you discard when you move a pointer is provably eliminated: if numbers[left] + numbers[right] > target, then numbers[left] + numbers[anything smaller than right] is also too big, since the array is sorted — so skipping every intermediate right value is completely safe.

  • 1-based indexing on the return value: LC 167 requires the 1-indexed positions [left + 1, right + 1], not the raw 0-indexed left/right; returning 0-indexed positions passes every trace by eye but fails the judge.

  • Moving both pointers after a mismatch: only the pointer the comparison rules out may move; moving both skips pairs, so numbers = [1, 2, 4, 6], target = 8 never checks 2 + 6.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Sorted array + find the pair that adds up to target, answered as 1-indexed positions, with only O(1) extra space: squeeze from both ends (Converging Pointers).

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: every pair that uses an index outside [left, right] cannot sum to target; one comparison of numbers[left] + numbers[right] with target rules out one more index.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Unsorted input: moving a pointer no longer raises or lowers the sum, so the squeeze can walk past the answer; the array must be sorted (LC 167 promises it).

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I'd use Converging Pointers: left starts at index 0 and right at the last index. Because numbers is sorted, if the sum is smaller than target, numbers[left] is already paired with the largest value still available, so it can never reach target and I move left forward. If the sum is too big, numbers[right] is already paired with the smallest value still available, so I move right back. Each step rules out one element for good, which is why the pair can never be skipped. The trap is input that is not sorted: then moving a pointer no longer raises or lowers the sum, so the squeeze can walk past the answer. For unsorted input I would use a hash map, or sort value and index pairs to keep the original positions. Here the input is sorted and 1-indexed, so I return left + 1 and right + 1. It's O(N) time and O(1) space."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the while loop: while left < right:. In each iteration, we evaluate current_sum = numbers[left] + numbers[right]. If the sum is too small, we increment left += 1; if too large, we decrement right -= 1. Because the distance (right - left) strictly decreases by 1 on every step, the loop executes at most N-1 times with O(1) arithmetic per step, yielding strictly O(N) time.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocation: The search is iterative with O(1) call stack frames. We only store two scalar pointer variables (left, right) and one scalar sum accumulator. Auxiliary space is strictly O(1) in-place.

Formal Recurrence Relation

∑ (delta L + delta R) ≤ N ⟹ O(N)

Derivation Progression

Search Interval Monotonicity

(right - left) decreases by 1 each step

Sorted order guarantees that adjusting left rightward increases the sum and adjusting right leftward decreases the sum.

O(1) Arithmetic Per Step

O(1) sum check

Testing current_sum == target takes constant time with zero inner loops.

Linear Bounded Termination

N - 1 maximum steps ⟹ O(N)

The pointers converge from opposite ends, inspecting at most N elements before finding the pair.

Variable Definitions

NNN

Total number of elements in the sorted array

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative loop with zero recursion frames

🔵 Auxiliary Heap

O(1) Only scalar pointers (left, right) in CPU registers

🟢 Output Space

O(1) Returns 2-element index list

Boundary Best / Worst Cases

Best Case

O(1)O(1)O(1) if outer pair sums to target.

Average Case

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

Worst Case

O(N)O(N)O(N) when pair is in the middle.

Pointer Invariant Transition Progression

Pointer Invariant Transition Progression
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "array is already sorted in non-decreasing order", "find two numbers that add up to target", "O(1) extra space". Converging opposite-direction pointers exploiting monotonic ordering.

CONSTRAINTS & BOUNDS

2≤N≤3×1042 \le N \le 3 \times 10^42≤N≤3×104. Time budget strictly O(N)O(N)O(N), space strictly O(1)O(1)O(1).

FAANG PRODUCTION TRAPS & EDGE CASES

Values stay within [−1000,1000][-1000, 1000][−1000,1000], so the sum cannot overflow here.

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