Skip to main content

8. Plus One

easyAsked at Wix

Add one to a number represented as a digit array; Wix uses similar arithmetic when bumping per-tenant template version numbers.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer and return the resulting array of digits.

Constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

Examples

Example 1

Input
[1,2,3]
Output
[1,2,4]

Example 2

Input
[9,9]
Output
[1,0,0]

Approaches

1. BigInt convert

Convert digits to a BigInt, add, split back.

Time
O(n)
Space
O(n)
return String(BigInt(digits.join(''))+1n).split('').map(Number);

Tradeoff:

2. Carry loop

Walk right-to-left propagating the carry.

Time
O(n)
Space
O(1)
function plusOne(digits){
  for(let i=digits.length-1;i>=0;i--){
    if(digits[i]<9){digits[i]++; return digits;}
    digits[i]=0;
  }
  return [1,...digits];
}

Tradeoff:

Wix-specific tips

Wix expects you to flag the all-nines overflow path explicitly — it's the kind of small edge case they care about for their template-version SemVer rollovers.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Plus One and other Wix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →