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•Backtracking & State Search
MediumLC 77

Combinations (LeetCode 77)

Target Frequency:AmazonGoogleMeta

What a combination is (assume nothing): a combination is a group of numbers where only which numbers are in the group matters, not their order. [1, 3] and [3, 1] are the same combination.

You get two integers n and k. Using the numbers 1 to n, list every group of exactly k different numbers. Each group appears once, and the groups can be returned in any order.

Worked Examples

Example 1
Input:n = 4, k = 2
Output:[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation: There are 6 ways to choose 2 of the 4 numbers. [2,1] is not listed separately because it is the same group as [1,2].
Example 2
Input:n = 1, k = 1
Output:[[1]]
Explanation: With a single number, the only group of size 1 is [1].

⚖️Formal Constraints & Bounds

  • 1 <= n <= 20

  • 1 <= k <= n

Deep-Dive & Conceptual Insights

Core Insight

Recursing with i + 1 means every group is built in increasing order, so each group is produced exactly once and no number repeats inside it. Recording only when len(path) == k (and returning right away) is the single change that turns the Subsets search into a fixed-size Combinations search.

Real-World Scenario & Production Applications

Picking every possible team of k people from a group of n, every set of k features to test together, or every k-item bundle from a catalog all mean listing each fixed-size group exactly once, without repeating the same group in a different order.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Forward-Only Number Choice

At each level, try only numbers from start to n. Anything below start was already passed over, which keeps every group in increasing order.

Mathematical Recurrence / Code Invariant
for i in range(start, n + 1):

Step-by-Step Execution Trace Table

Code / Blueprint
[ ] (start=1)
/ | \ \
choose 1 choose 2 choose 3 choose 4
/ | \ \
[1] (start=2) [2] (start=3) [3] (start=4) [4] (start=5)
/ | \ / \ | (no numbers left)
[1,2] [1,3] [1,4] [2,3] [2,4] [3,4]
✅ ✅ ✅ ✅ ✅ ✅ (len(path) == k = 2: RECORD)
 
Step-by-Step Decision Trace
  1. Step 1 (Root, start=1): path = [], size 0 < 2. Try numbers 1 to 4.
  2. Step 2 (Choose 1): path = [1], recurse with start = 2.
  3. Step 3 (Choose 2): path = [1, 2]. Size is 2 →\rightarrow→ Record [1, 2] ✅ and return.
  4. Step 4 (Pop 2, choose 3): path = [1, 3] →\rightarrow→ Record [1, 3] ✅.
  5. Step 5 (Pop 3, choose 4): path = [1, 4] →\rightarrow→ Record [1, 4] ✅. The loop at [1] ends.
  6. Step 6 (Pop 1, choose 2 at the root): path = [2], recurse with start = 3. Record [2, 3] ✅ and [2, 4] ✅.
  7. Step 7 (Choose 3 at the root): path = [3], recurse with start = 4. Record [3, 4] ✅.
  8. Step 8 (Choose 4 at the root): path = [4], recurse with start = 5. No numbers are left, so this branch records nothing. Search Complete: Output [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]], which is (42)=6\binom{4}{2} = 6(24​)=6 groups.
Full Walkthrough8 Steps
Input:n = 4, k = 2Expected:[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
1🌱 ROOT(Root, start=1)
path = []
path = [], size 0 < 2. Try numbers 1 to 4.
2🟢 CHOOSE(Choose 1)
path = [1]
path = [1], recurse with start = 2.
3✅ RECORD / GOAL(Choose 2)
path = [1, 2]
path = [1, 2]. Size is 2 →\rightarrow→ Record [1, 2] ✅ and return.
4⚡ FIFO DRAIN(Pop 2, choose 3)
path = [1, 3]
path = [1, 3] →\rightarrow→ Record [1, 3] ✅.
5⚡ FIFO DRAIN(Pop 3, choose 4)
path = [1, 4]
path = [1, 4] →\rightarrow→ Record [1, 4] ✅. The loop at [1] ends.
6🌱 ROOT(Pop 1, choose 2 at the root)
path = [2]
path = [2], recurse with start = 3. Record [2, 3] ✅ and [2, 4] ✅.
7🌱 ROOT(Choose 3 at the root)
path = [3]
path = [3], recurse with start = 4. Record [3, 4] ✅.
8🌱 ROOT(Choose 4 at the root)
path = [4]
path = [4], recurse with start = 5. No numbers are left, so this branch records nothing. Search Complete: Output [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]], which is (42)=6\binom{4}{2} = 6(24​)=6 groups.
Code Shape
Archetype Code Shape
Python
1results, path = [], []
2def backtrack(start):
3 if len(path) == k:
4 results.append(list(path))
5 return
6 need = k - len(path)
7 for i in range(start, n - need + 2):
8 path.append(i)
9 backtrack(i + 1)
10 path.pop()
11backtrack(1)
12return results

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Subsets record a group at every node of the decision tree, so you get every size from empty to full. Combinations ask for groups of one fixed size k. The loop and the i + 1 are exactly the same as in Subsets; the only change is when you record: only once len(path) == k, and then you stop going deeper.

🏀 The Analogy: Picking a Team of k Players

Think of n players with jersey numbers 1 to n, and you must list every possible team of k. To avoid writing the same team twice, always list a team's jerseys from smallest to largest. So after you pick jersey 3, you only look at jerseys 4 and up. The team [1, 3] gets written once; [3, 1] never appears, because nobody ever looks backward. As soon as a team has k players, write it down and step back to try the next player.

🔑 The Key Idea in the Code

backtrack(i + 1) does two jobs at once: it keeps i from being used twice in the same group, and it makes every group appear in increasing order only, so no group is ever listed twice.

✂️ The Loop Bound: Stop When There Aren't Enough Numbers Left

If the group still needs need = k - len(path) numbers, starting at a number too close to n can never fill it. The largest useful start is n - need + 1, so the loop is for i in range(start, n - need + 2). The answer is the same as looping up to n; the dead-end branches are simply never entered. Cut the bound one lower and every group that ends in n is lost.

  • Recording before the group is full: Recording at every node (the Subsets habit) returns groups of every size. Record only when len(path) == k, and return right after, because a full group can't grow.

  • Looping from 0 instead of start: Starting each loop at the first number builds [1, 2] and [2, 1] as two different groups. Always loop from start and recurse with i + 1.

  • Appending the live path: results.append(path) stores a reference to the one shared list, which is empty when the search ends. Append list(path).

  • Off-by-one in the pruning bound: The optional early stop allows i up to n - (k - len(path)) + 1 inclusive, so the range end is n - (k - len(path)) + 2. One less skips valid groups.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"Return all combinations of k numbers" + "order does not matter" + small n (n <= 20) + a fixed group size k.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: path is always an increasing list of numbers from 1..n, and every number after path[-1] is still available. Complete when len(path) == k: record a copy and return, since a full group can't grow.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Recursing with start + 1 instead of i + 1 -- the next level then restarts too early, so a number can repeat ([2, 2]) and the same group appears in two orders ([2, 3] and [3, 2]).

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I'd use Combinations. I build each group in increasing order with backtrack(start): the loop tries every number i from start upward, appends it to path, recurses, then pops it. A group of numbers has exactly one increasing order, so building only in that order produces each group once and never both [1, 2] and [2, 1]. When path has k numbers I save a copy and return. I also stop the loop early when too few numbers are left to fill the group, which removes dead branches without changing the answer. The trap is the recursive call: it must be backtrack(i + 1), not backtrack(start + 1), because the next pick has to come after the number I just picked; with start + 1 and n = 4, k = 2, I get [3, 2] next to [2, 3]. There are C(n, k) groups and each copy costs k, so it's O(k times C(n, k)) time and O(k) extra space for path and the recursion."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(k · C(n, k))

Look at the execution steps: the search records exactly C(n, k) groups, and copying each one with list(path) costs k. The internal nodes of the tree are prefixes of those groups, so the total work is O(k · C(n, k)).

SPACE COMPLEXITY

O(k) Call Stack

The recursion never goes deeper than k levels, and path holds at most k numbers. The output itself holds C(n, k) groups of k numbers each.

Formal Recurrence Relation

T(n,k)=O(k⋅(nk))T(n, k) = O(k \cdot \binom{n}{k})T(n,k)=O(k⋅(kn​))

Derivation Progression

Leaf Count

Leaves = C(n, k)

Every recorded group is one leaf of the tree, and each group is produced exactly once.

Tree Depth Bound

Max Depth = k

A group is recorded (and the branch stops) as soon as it has k numbers.

Copy Cost

k per recorded group

list(path) copies k numbers each time a group is recorded.

Variable Definitions

nnn

Largest number available (choose from 1..n)

kkk

Size of each group

Memory Architecture & Bounds

🟣 Call Stack

O(k) Call stack depth

🔵 Auxiliary Heap

O(k) Current group (path)

🟢 Output Space

O(k · C(n, k)) Result groups

Boundary Best / Worst Cases

Best Case

O(n)O(n)O(n) when k=1k = 1k=1 or k=nk = nk=n.

Average Case

O(k⋅(nk))O(k \cdot \binom{n}{k})O(k⋅(kn​)).

Worst Case

O(k⋅(nk))O(k \cdot \binom{n}{k})O(k⋅(kn​)), largest when k≈n/2k \approx n/2k≈n/2.

Decision & State-Space Search Tree

Decision & State-Space Search Tree
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "all possible combinations of k numbers from [1, n]", "order does not matter". Canonical combinations pattern: keep the candidate index strictly increasing so no group is built twice.

CONSTRAINTS & BOUNDS

n≤20,k≤nn \le 20, k \le nn≤20,k≤n. Output size is (nk)\binom{n}{k}(kn​), at most (2010)=184,756\binom{20}{10} = 184{,}756(1020​)=184,756 groups; the output, not the search, sets the cost.

FAANG PRODUCTION TRAPS & EDGE CASES

At scale memory fails first: (nk)\binom{n}{k}(kn​) groups of kkk numbers each. When the caller only iterates (for example, feeding each group to a job), stream the groups with a generator and yield a copy of path.

Theory Context•Backtracking & State Search
MediumLC 77

Combinations (LeetCode 77)

Target Frequency:AmazonGoogleMeta

What a combination is (assume nothing): a combination is a group of numbers where only which numbers are in the group matters, not their order. [1, 3] and [3, 1] are the same combination.

You get two integers n and k. Using the numbers 1 to n, list every group of exactly k different numbers. Each group appears once, and the groups can be returned in any order.

Worked Examples

Example 1
Input:n = 4, k = 2
Output:[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Explanation: There are 6 ways to choose 2 of the 4 numbers. [2,1] is not listed separately because it is the same group as [1,2].
Example 2
Input:n = 1, k = 1
Output:[[1]]
Explanation: With a single number, the only group of size 1 is [1].

⚖️Formal Constraints & Bounds

  • 1 <= n <= 20

  • 1 <= k <= n

Deep-Dive & Conceptual Insights

Core Insight

Recursing with i + 1 means every group is built in increasing order, so each group is produced exactly once and no number repeats inside it. Recording only when len(path) == k (and returning right away) is the single change that turns the Subsets search into a fixed-size Combinations search.

Real-World Scenario & Production Applications

Picking every possible team of k people from a group of n, every set of k features to test together, or every k-item bundle from a catalog all mean listing each fixed-size group exactly once, without repeating the same group in a different order.

Subproblems & Recurrence Decomposition3 Phases

🧩Subproblem 1: Forward-Only Number Choice

At each level, try only numbers from start to n. Anything below start was already passed over, which keeps every group in increasing order.

Mathematical Recurrence / Code Invariant
for i in range(start, n + 1):

Step-by-Step Execution Trace Table

Code / Blueprint
[ ] (start=1)
/ | \ \
choose 1 choose 2 choose 3 choose 4
/ | \ \
[1] (start=2) [2] (start=3) [3] (start=4) [4] (start=5)
/ | \ / \ | (no numbers left)
[1,2] [1,3] [1,4] [2,3] [2,4] [3,4]
✅ ✅ ✅ ✅ ✅ ✅ (len(path) == k = 2: RECORD)
 
Step-by-Step Decision Trace
  1. Step 1 (Root, start=1): path = [], size 0 < 2. Try numbers 1 to 4.
  2. Step 2 (Choose 1): path = [1], recurse with start = 2.
  3. Step 3 (Choose 2): path = [1, 2]. Size is 2 →\rightarrow→ Record [1, 2] ✅ and return.
  4. Step 4 (Pop 2, choose 3): path = [1, 3] →\rightarrow→ Record [1, 3] ✅.
  5. Step 5 (Pop 3, choose 4): path = [1, 4] →\rightarrow→ Record [1, 4] ✅. The loop at [1] ends.
  6. Step 6 (Pop 1, choose 2 at the root): path = [2], recurse with start = 3. Record [2, 3] ✅ and [2, 4] ✅.
  7. Step 7 (Choose 3 at the root): path = [3], recurse with start = 4. Record [3, 4] ✅.
  8. Step 8 (Choose 4 at the root): path = [4], recurse with start = 5. No numbers are left, so this branch records nothing. Search Complete: Output [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]], which is (42)=6\binom{4}{2} = 6(24​)=6 groups.
Full Walkthrough8 Steps
Input:n = 4, k = 2Expected:[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
1🌱 ROOT(Root, start=1)
path = []
path = [], size 0 < 2. Try numbers 1 to 4.
2🟢 CHOOSE(Choose 1)
path = [1]
path = [1], recurse with start = 2.
3✅ RECORD / GOAL(Choose 2)
path = [1, 2]
path = [1, 2]. Size is 2 →\rightarrow→ Record [1, 2] ✅ and return.
4⚡ FIFO DRAIN(Pop 2, choose 3)
path = [1, 3]
path = [1, 3] →\rightarrow→ Record [1, 3] ✅.
5⚡ FIFO DRAIN(Pop 3, choose 4)
path = [1, 4]
path = [1, 4] →\rightarrow→ Record [1, 4] ✅. The loop at [1] ends.
6🌱 ROOT(Pop 1, choose 2 at the root)
path = [2]
path = [2], recurse with start = 3. Record [2, 3] ✅ and [2, 4] ✅.
7🌱 ROOT(Choose 3 at the root)
path = [3]
path = [3], recurse with start = 4. Record [3, 4] ✅.
8🌱 ROOT(Choose 4 at the root)
path = [4]
path = [4], recurse with start = 5. No numbers are left, so this branch records nothing. Search Complete: Output [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]], which is (42)=6\binom{4}{2} = 6(24​)=6 groups.
Code Shape
Archetype Code Shape
Python
1results, path = [], []
2def backtrack(start):
3 if len(path) == k:
4 results.append(list(path))
5 return
6 need = k - len(path)
7 for i in range(start, n - need + 2):
8 path.append(i)
9 backtrack(i + 1)
10 path.pop()
11backtrack(1)
12return results

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

Subsets record a group at every node of the decision tree, so you get every size from empty to full. Combinations ask for groups of one fixed size k. The loop and the i + 1 are exactly the same as in Subsets; the only change is when you record: only once len(path) == k, and then you stop going deeper.

🏀 The Analogy: Picking a Team of k Players

Think of n players with jersey numbers 1 to n, and you must list every possible team of k. To avoid writing the same team twice, always list a team's jerseys from smallest to largest. So after you pick jersey 3, you only look at jerseys 4 and up. The team [1, 3] gets written once; [3, 1] never appears, because nobody ever looks backward. As soon as a team has k players, write it down and step back to try the next player.

🔑 The Key Idea in the Code

backtrack(i + 1) does two jobs at once: it keeps i from being used twice in the same group, and it makes every group appear in increasing order only, so no group is ever listed twice.

✂️ The Loop Bound: Stop When There Aren't Enough Numbers Left

If the group still needs need = k - len(path) numbers, starting at a number too close to n can never fill it. The largest useful start is n - need + 1, so the loop is for i in range(start, n - need + 2). The answer is the same as looping up to n; the dead-end branches are simply never entered. Cut the bound one lower and every group that ends in n is lost.

  • Recording before the group is full: Recording at every node (the Subsets habit) returns groups of every size. Record only when len(path) == k, and return right after, because a full group can't grow.

  • Looping from 0 instead of start: Starting each loop at the first number builds [1, 2] and [2, 1] as two different groups. Always loop from start and recurse with i + 1.

  • Appending the live path: results.append(path) stores a reference to the one shared list, which is empty when the search ends. Append list(path).

  • Off-by-one in the pruning bound: The optional early stop allows i up to n - (k - len(path)) + 1 inclusive, so the range end is n - (k - len(path)) + 2. One less skips valid groups.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

"Return all combinations of k numbers" + "order does not matter" + small n (n <= 20) + a fixed group size k.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: path is always an increasing list of numbers from 1..n, and every number after path[-1] is still available. Complete when len(path) == k: record a copy and return, since a full group can't grow.

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Recursing with start + 1 instead of i + 1 -- the next level then restarts too early, so a number can repeat ([2, 2]) and the same group appears in two orders ([2, 3] and [3, 2]).

The 60-Second Interview Pitch

Say this out loud before you type a single line

"I'd use Combinations. I build each group in increasing order with backtrack(start): the loop tries every number i from start upward, appends it to path, recurses, then pops it. A group of numbers has exactly one increasing order, so building only in that order produces each group once and never both [1, 2] and [2, 1]. When path has k numbers I save a copy and return. I also stop the loop early when too few numbers are left to fill the group, which removes dead branches without changing the answer. The trap is the recursive call: it must be backtrack(i + 1), not backtrack(start + 1), because the next pick has to come after the number I just picked; with start + 1 and n = 4, k = 2, I get [3, 2] next to [2, 3]. There are C(n, k) groups and each copy costs k, so it's O(k times C(n, k)) time and O(k) extra space for path and the recursion."

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(k · C(n, k))

Look at the execution steps: the search records exactly C(n, k) groups, and copying each one with list(path) costs k. The internal nodes of the tree are prefixes of those groups, so the total work is O(k · C(n, k)).

SPACE COMPLEXITY

O(k) Call Stack

The recursion never goes deeper than k levels, and path holds at most k numbers. The output itself holds C(n, k) groups of k numbers each.

Formal Recurrence Relation

T(n,k)=O(k⋅(nk))T(n, k) = O(k \cdot \binom{n}{k})T(n,k)=O(k⋅(kn​))

Derivation Progression

Leaf Count

Leaves = C(n, k)

Every recorded group is one leaf of the tree, and each group is produced exactly once.

Tree Depth Bound

Max Depth = k

A group is recorded (and the branch stops) as soon as it has k numbers.

Copy Cost

k per recorded group

list(path) copies k numbers each time a group is recorded.

Variable Definitions

nnn

Largest number available (choose from 1..n)

kkk

Size of each group

Memory Architecture & Bounds

🟣 Call Stack

O(k) Call stack depth

🔵 Auxiliary Heap

O(k) Current group (path)

🟢 Output Space

O(k · C(n, k)) Result groups

Boundary Best / Worst Cases

Best Case

O(n)O(n)O(n) when k=1k = 1k=1 or k=nk = nk=n.

Average Case

O(k⋅(nk))O(k \cdot \binom{n}{k})O(k⋅(kn​)).

Worst Case

O(k⋅(nk))O(k \cdot \binom{n}{k})O(k⋅(kn​)), largest when k≈n/2k \approx n/2k≈n/2.

Decision & State-Space Search Tree

Decision & State-Space Search Tree
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

Triggers: "all possible combinations of k numbers from [1, n]", "order does not matter". Canonical combinations pattern: keep the candidate index strictly increasing so no group is built twice.

CONSTRAINTS & BOUNDS

n≤20,k≤nn \le 20, k \le nn≤20,k≤n. Output size is (nk)\binom{n}{k}(kn​), at most (2010)=184,756\binom{20}{10} = 184{,}756(1020​)=184,756 groups; the output, not the search, sets the cost.

FAANG PRODUCTION TRAPS & EDGE CASES

At scale memory fails first: (nk)\binom{n}{k}(kn​) groups of kkk numbers each. When the caller only iterates (for example, feeding each group to a job), stream the groups with a generator and yield a copy of path.

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