‹ DS&A interview · Socratic
DSA · Backtracking · #52

Combinations

Module 52 · difficulty 3/5·30:00starts on first keystroke

Given two integers `n` and `k`, return all possible combinations of `k` numbers chosen from the range `[1, n]`. Implement `combine(n, k)` returning an array of arrays. You may return the answer in any order.

Examples
  • n = 4, k = 2 [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]All ways to pick 2 distinct numbers from 1..4.
  • n = 1, k = 1 [[1]]Only one element to choose.
  • n = 3, k = 3 [[1,2,3]]k equals n, so only the full set.
Constraints
  • · 1 <= n <= 20
  • · 1 <= k <= n
  • · Each combination is a set of distinct numbers (no repeats).
  • · The numbers within each combination should be in increasing order.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.