66. Plus One
easyAsked at CitadelPlus One probes your carry-propagation instinct — the same mechanics underpin arbitrary-precision arithmetic libraries used in financial systems for exact decimal calculations. Citadel interviewers watch whether you handle the all-nines edge case (where a new leading digit is needed) cleanly and without special-casing.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Citadel loops.
- Glassdoor (2025-10)— Citadel SWE reports list Plus One and similar array-manipulation problems as occasional warm-ups in technical screens.
- Blind (2025-07)— Citadel early-round coding assessments noted to include straightforward array problems testing edge-case awareness.
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 zeros. Increment the large integer by one and return the resulting array of digits.
Constraints
1 <= digits.length <= 1000 <= digits[i] <= 9digits does not contain any leading zero except for the number 0 itself.
Examples
Example 1
digits = [1,2,3][1,2,4]Explanation: 123 + 1 = 124.
Example 2
digits = [4,3,2,1][4,3,2,2]Explanation: 4321 + 1 = 4322.
Example 3
digits = [9,9,9][1,0,0,0]Explanation: 999 + 1 = 1000 — a new leading digit is required.
Approaches
1. Right-to-left carry propagation
Walk from the least significant digit. If digit < 9, increment and return. If digit = 9, set to 0 and carry. If carry propagates past index 0, prepend a 1.
- Time
- O(n)
- Space
- O(1) in-place, or O(n) if a new array is needed
function plusOne(digits) {
for (let i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits; // no carry, done
}
digits[i] = 0; // carry: set to 0, continue
}
// All digits were 9 — need a new leading 1
return [1, ...digits]; // e.g. [1,0,0,0]
}Tradeoff: O(n) time, O(1) space in the common case (no all-nines). The all-nines path allocates a new array of length n+1, but this is unavoidable. The early return on non-carry makes the common case very fast.
Citadel-specific tips
Mention the all-nines edge case before you start coding — it shows you think about boundary conditions upfront. In financial arithmetic libraries, carry propagation is critical for exact decimal arithmetic (e.g., adding one cent to $9.99 → $10.00 without floating-point error). Citadel may generalize: 'What if you need to add an arbitrary integer k, not just 1?' — that requires a full digit-by-digit addition loop with carry.
Common mistakes
- Converting the array to a JavaScript number — this fails for arrays longer than 15-16 digits due to floating-point precision limits.
- Not handling the all-nines case — forgetting to prepend a 1 when the carry propagates past index 0.
- Using unshift(1) instead of [1, ...digits] — both work but unshift mutates in O(n); the spread creates a new array. Either is fine; just be explicit about the tradeoff.
- Starting the loop from index 0 instead of digits.length - 1 — the addition starts from the least significant digit.
Follow-up questions
An interviewer at Citadel may pivot to one of these next:
- Add Binary (LC 67) — same carry logic but in base 2.
- Add Strings (LC 415) — two arbitrary-precision strings representing integers.
- How would you implement a full arbitrary-precision addition (plus any k)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why not just convert to a number, add 1, and convert back?
JavaScript numbers are IEEE-754 doubles with 53-bit mantissa. For arrays of more than 15-16 digits, integer precision is lost. The array-based approach handles arbitrarily large integers exactly.
When does the all-nines path trigger?
When every digit is 9 (e.g., 9, 99, 999). After the loop, all digits are 0 and we prepend 1. This is the only case where output length > input length.
Is the early return optimization significant?
Yes — in practice most numbers don't end in 9. The early return means the common case touches only one element and returns immediately, which is relevant in tight loops.