Plus One solution explained

To add one to a number stored as an array of digits, walk from the last digit toward the first: if a digit is below 9, increase it and return; if it is 9, set it to 0 and carry to the next digit. If every digit was 9, put a 1 in front. The Plus One solution runs in O(n) time in the worst case, stops early in the common case, and never converts the digits into a built-in number type.

By Published 6 min read

The task, restated

A non-negative integer arrives as a list of its decimal digits, highest place first, with no leading zeros except for the number zero itself. Return a list in the same format for that integer plus one. The list may be long, far longer than any built-in integer type could hold.

Examples made up for this page
Input digitsOutput digitsWhat happened
[4, 0, 9][4, 1, 0]The 9 rolls to 0 and the carry stops at the 0, which becomes 1
[2, 9, 9, 9][3, 0, 0, 0]Three carries, absorbed by the 2
[9, 9][1, 0, 0]Every digit rolls over; the list grows by one
[7][8]No carry at all
[0][1]Zero is the one input that starts with 0
Examples made up for this page. Scroll sideways on a small screen.

Clarify whether you may change the input list or must return a new one, and whether the list can be empty (the usual answer is no).

How interviewers use it

Plus One looks too easy, which is the point. Interviewers watch for the candidate who joins the digits into an integer, adds one and splits it back. In Python that happens to work because integers grow without limit, so the real test becomes whether you can say why it would fail in most other languages and write the carry loop anyway. It is also a check on early exit and on the all-nines case.

  • Add any integer k instead of 1. Add k to the last digit and push the carry left; the carry can now be more than 1.
  • Add two digit lists. Walk both from the right with a carry, the same loop with two inputs.
  • Digits are stored least significant first. Walk from the front and append when the carry survives.
  • The number is a singly linked list, most significant first. Reverse it, add, reverse back; or find the last non-9 node and work from there.

Brute force: convert, add, convert back

Join the digits into a string, parse it as an integer, add one, and split the result into digits. In Python this is correct because its integers have arbitrary precision. In JavaScript, a normal Number holds integers exactly only up to 2 to the power of 53 minus 1. Beyond that, values round silently.

def plus_one_via_int(digits):
    value = int("".join(map(str, digits))) + 1
    return [int(ch) for ch in str(value)]
Works in Python, which has unbounded integers. Used here as the test oracle.
const digits = [9, 0, 0, 7, 1, 9, 9, 2, 5, 4, 7, 4, 0, 9, 9, 3];
console.log(String(Number(digits.join("")) + 1));
// 9007199254740992  -- smaller than the input, not one larger
What goes wrong with an ordinary Number (output printed by node).

BigInt would fix the JavaScript version, but the interviewer almost always wants the carry loop, because it shows you understand the arithmetic rather than the library.

The efficient approach: carry from the right

Adding one only changes the trailing run of 9s and the digit just before them. Walk from the last index backward. A digit below 9 takes the increment and you are done; nothing to its left changes. A 9 becomes 0 and the carry moves one place left. If the loop finishes, every digit was 9, so the answer is a 1 followed by zeros, one digit longer than the input.

def plus_one(digits):
    out = digits[:]
    for i in range(len(out) - 1, -1, -1):
        if out[i] < 9:
            out[i] += 1
            return out
        out[i] = 0
    return [1] + out
Python: copy, then carry from the right. O(n) worst case, early exit otherwise.
function plusOne(digits) {
  const out = digits.slice();
  for (let i = out.length - 1; i >= 0; i--) {
    if (out[i] < 9) {
      out[i]++;
      return out;
    }
    out[i] = 0;
  }
  out.unshift(1);
  return out;
}
JavaScript: slice copies the input so the caller's array is untouched.

If the interviewer allows changing the input, drop the copy. The all-nines case still needs a new, longer list, or an insert at the front, which is O(n).

Time and space

Complexity for n digits
VersionTimeExtra space
Convert to integer and back (Python)O(n) to O(n²) depending on the conversionO(n)
Carry loop, copying the inputO(n)O(n) for the copy
Carry loop, in placeO(k) where k is the number of trailing 9s, O(n) when all are 9O(1), or O(n) when the list must grow
Complexity for n digits. Scroll sideways on a small screen.

Large integer conversions in many languages are not linear, which is one more reason to prefer the loop when inputs can be long.

Edge cases

  • All nines, which lengthens the result.
  • A single digit, including 9 and 0.
  • A trailing 9 with a non-9 before it, such as [1, 9].
  • Very long inputs, which rule out built-in number types in most languages.
  • No trailing 9 at all, where the loop runs once.

Mistakes to avoid

  • Relying on number conversion in a language with fixed-size integers or floating-point numbers.
  • Forgetting the all-nines case and returning [0, 0] for [9, 9].
  • Continuing the loop after the carry is absorbed, which wastes work and sometimes adds one twice.
  • Changing the caller's list without saying so.
  • Walking from the front, where you cannot know about a carry yet.

How to explain it out loud

Fictional example

Showing you know why the shortcut is risky
Candidate
I could join the digits into an integer, but these lists can be longer than any fixed-size integer, and in JavaScript a Number loses precision after about 16 digits. So I will add like on paper.
Candidate
From the right: a digit under 9 just goes up by one and I am done. A 9 becomes 0 and I carry. If I fall off the front, all digits were 9, so I put a 1 in front.
Candidate
Worst case is linear, when everything is 9. Usually it stops after one or two digits.

Happy Number also works digit by digit, using divmod instead of a list. Pow(x, n) is the other arithmetic problem in this set, about doing far fewer multiplications. For carrying across linked-list nodes rather than array slots, the same carry logic drives adding two numbers stored as lists.

Where Natively fits

For practice on Mac or Windows, Natively can read a short array problem like this from your screen and point out the overflow trap before you take the shortcut. It transcribes your practice session as well, so you can check that you mentioned the all-nines case out loud rather than only in code. See the AI interview assistant.

Questions people ask

How do you add one to an array of digits?

Walk from the last digit. If it is below 9, add one and return. If it is 9, set it to 0 and carry left. If every digit was 9, put a 1 in front.

Why not convert the digits to an integer?

The list can be longer than a fixed-size integer, and a JavaScript Number loses precision beyond 2 to the 53rd power. Python integers are unbounded, but interviewers still expect the carry loop.

What is the time complexity of Plus One?

O(n) in the worst case, when all digits are 9. Usually the loop stops after the first digit below 9.

What is the edge case in Plus One?

All nines, such as [9, 9], which must become [1, 0, 0], one digit longer than the input.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP