Skip to main content

7. Plus One

easyAsked at Salesforce

Increment a number represented as an array of digits by one. Salesforce asks this to verify you handle carry propagation cleanly — a building block for their financial-precision arithmetic.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Asked occasionally on Salesforce billing-platform phone screens.
  • LeetCode Discuss (2025)Used as a warmup before BigDecimal arithmetic follow-ups.

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.

Examples

Example 1

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

Explanation: Input is 123, output is 124.

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 and back

Join digits, parse as BigInt, add 1, convert back to array.

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

Tradeoff: Works but Salesforce wants to see manual carry — they explicitly ask this to test that skill, not BigInt knowledge.

2. In-place carry from right

Walk from right to left. If digit < 9, increment and return. Else set to 0 and carry. If you exit the loop, prepend a 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: Short-circuit early-return makes this O(1) amortized for most inputs. The 'all 9s' case correctly prepends 1.

Salesforce-specific tips

Salesforce uses this style of digit-by-digit arithmetic in their currency code for arbitrary-precision financial values. They grade on whether you spot the early-return optimization (digit < 9) and handle the all-9s case. Bonus signal: mention that returning early on the first non-9 makes the algorithm O(1) amortized.

Common mistakes

  • Using digits.unshift(1) inside the loop — O(n) per call, total O(n^2).
  • Forgetting the all-9s case — returns [0,0,0] instead of [1,0,0,0] for input [9,9,9].
  • Converting via Number() instead of BigInt() — overflows for 16+ digit inputs.

Follow-up questions

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

  • Add Two Numbers (LC 2 — linked list version).
  • Multiply Strings (LC 43 — generalize from +1 to *).
  • Add Binary (LC 67 — same pattern, base 2).

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 is BigInt the wrong answer here?

Because the interviewer is specifically testing carry-propagation logic. Reaching for BigInt is the lazy answer that signals you'd rather avoid the algorithmic exercise.

Why prepend instead of in-place?

The result of all-9s + 1 is one digit longer, so the array must grow. Prepending [1, ...digits] is cleaner than mutating length manually.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →