Skip to main content

7. Plus One

easyAsked at N26

Increment a number represented as a digit array. N26 uses this to surface carry-handling intuition, a precursor to integer-cents arithmetic for monetary amounts.

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

Problem

You are given a large integer represented as an integer array digits, where each digits[i] is the i-th digit of the integer. Increment the integer by one and return the resulting array of digits. Carry must be propagated correctly across leading positions.

Constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain leading zeros (unless it is just [0])

Examples

Example 1

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

Example 2

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

Approaches

1. BigInt conversion

Stringify, parse as BigInt, increment, restringify.

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

Tradeoff:

2. In-place carry walk

Walk right to left, propagating carry; prepend 1 if it survives.

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:

N26-specific tips

N26 will probe whether you store money as integer cents — flag that carry propagation matters here exactly as in EUR cent arithmetic.

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 N26 interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →