Skip to content
EasyArraysAI interview only

Plus One

Asked atgoogleamazonapplemicrosoftadobe

01 · Problem

A non-negative integer is stored as an array digits, where each element is a single decimal digit and the most significant digit comes first. The number has no leading zeros, except for the number 0 itself, which is represented as [0].

Add one to the number and return the digits of the result in the same format.

02 · Examples

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

The array represents 123, and 123 + 1 = 124.

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

The array represents 4321, and 4321 + 1 = 4322.

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

99 + 1 = 100; the carry ripples through every digit and adds a new leading 1.

03 · Constraints

  • 011 <= digits.length <= 100
  • 020 <= digits[i] <= 9
  • 03digits has no leading zeros unless it is exactly [0]

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.