Combinations

MediumBacktrackingbacktracking

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given two integers n and k, return every way to choose k distinct numbers from the range 1..n. Write each combination as a list in **ascending** order. The combinations may be returned in any order.

Examples

Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
There are C(4,2) = 6 ways.
Input: n = 1, k = 1
Output: [[1]]
The only choice is [1].

Constraints

  • 1 <= n <= 20
  • 1 <= k <= n
  • C(n, k) <= 10^4 in the tests

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More backtracking problems