Additive Number

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

A digit string is **additive** if it can be cut into a sequence of at least three numbers where every number after the first two equals the sum of the two numbers before it. No number in the sequence may have a leading zero, although the single digit "0" is allowed. All digits of num must be used, in order. Return true if num is additive, otherwise false. Numbers can exceed 64-bit range, so reason about them carefully in languages without big integers.

Examples

Input: num = "112358"
Output: true
1, 1, 2, 3, 5, 8: each number is the sum of the previous two.
Input: num = "199100199"
Output: true
1, 99, 100, 199: 1 + 99 = 100 and 99 + 100 = 199.

Constraints

  • `1 <= num.length <= 35`
  • `num` consists only of digits

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