Skip to main content

8. Plus One

easyAsked at Datadog

Given an array of digits representing an integer, increment by one and return the new digit array. Datadog uses this to gauge edge-case discipline — the 999 → 1000 case is the same shape as carry propagation in their atomic-counter rollup.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Used at Datadog as a quick edge-case filter.
  • LeetCode Discuss (2025-10)Mentioned in Datadog OA pool.

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. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. 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.

Examples

Example 1

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

Example 2

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

Example 3

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

Approaches

1. Convert to BigInt, increment, convert back

Join digits, parse as BigInt, +1, stringify, split.

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

Tradeoff: Works but signals you avoid the digit-by-digit logic. Interviewers see this as a dodge.

2. Carry propagation in place (optimal)

Walk right to left. If digit < 9, increment and return. If 9, set to 0 and carry. If all carry through, prepend 1.

Time
O(n)
Space
O(1) extra (O(n) only when all 9s)
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: O(1) extra space in the common case; O(n) only when every digit is 9. Same logic Datadog uses for carry in their counter aggregation.

Datadog-specific tips

Datadog interviewers grade on the [9,9,9] edge case. If you don't think about overflow into a new most-significant digit, you'll get pushed. Bonus: mention how this generalizes to base-256 byte arithmetic for binary counters.

Common mistakes

  • Forgetting the all-nines case — returning [0,0,0,0] instead of [1,0,0,0].
  • Iterating left-to-right instead of right-to-left — addition propagates right-to-left.
  • Allocating a new array unconditionally — wastes memory in the common case.

Follow-up questions

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

  • Add Strings (LC 415) — generalize to adding two digit arrays.
  • Multiply Strings (LC 43) — same carry-propagation pattern.
  • Add Two Numbers (LC 2) — linked-list version.

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 iterate right to left?

Carry propagates from the least significant digit (rightmost) upward. Going left to right means you don't know yet whether to add 1 to the current digit.

When is extra space needed?

Only when every digit is 9 — then the result has one more digit than the input.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →