Combination Sum III

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

Find every set of exactly k **distinct** digits from 1 to 9 whose sum is n. Write each set as a list in **ascending** order. The sets may be returned in any order; return an empty list if none exist.

Examples

Input: k = 3, n = 7
Output: [[1,2,4]]
1 + 2 + 4 = 7 is the only option.
Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Three sets of three digits sum to 9.
Input: k = 4, n = 1
Output: []
Four distinct positive digits sum to at least 10.

Constraints

  • 2 <= k <= 9
  • 1 <= n <= 60

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