Skip to main content

8. Plus One

easyAsked at Reddit

Add one to a big integer represented as an array of digits. Reddit asks this to test careful carry-handling — the same kind of mental model needed for monotonic karma counters that survive past 2^31.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit infra team uses this as a 5-minute warm-up before harder distributed-counter questions.

Problem

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant. Increment the large integer by one and return the resulting array of digits.

Constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits does not contain any leading 0's, except for the number 0 itself.

Examples

Example 1

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

Example 2

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

Example 3

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

Approaches

1. Convert to BigInt, add 1, convert back

Join digits to string, parse to BigInt, add, split back.

Time
O(n)
Space
O(n)
function plusOne(digits) {
  const n = BigInt(digits.join('')) + 1n;
  return n.toString().split('').map(Number);
}

Tradeoff: Works but defeats the purpose. Interviewers flag this as 'avoiding the problem'.

2. In-place carry from right (optimal)

Walk from the last digit. If 9, set to 0 and continue. Otherwise increment and return. If we fall off the left, prepend 1.

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: Single pass, O(1) extra (other than the all-9s case).

Reddit-specific tips

Reddit interviewers expect you to ask: 'can we mutate the input?' If yes, the in-place version is ideal. Bonus signal: mention how their karma counter would face this exact problem if stored as a base-10 array for cross-shard merging.

Common mistakes

  • Forgetting the all-9s case (returns [1, 0, 0, ...]).
  • Using parseInt — overflows for digits.length > 16.
  • Iterating left-to-right (carry direction is right-to-left).

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Add two big integers represented as arrays (LC 415).
  • Add binary strings (LC 67).
  • Multiply two big integers as strings (LC 43).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why not use Number.parseInt?

JavaScript's Number is a 64-bit float — only safe up to 2^53 - 1. The constraint allows 100 digits.

Could we use Array.prototype.unshift instead of spread?

Yes, but unshift is O(n) anyway. Spread is clearer.

Companies that also ask Plus One