Skip to main content

66. Plus One

easyAsked at DRW

DRW uses Plus One to probe carry-propagation logic — a primitive that appears in arbitrary-precision integer arithmetic, price tick increments, and sequence-number rollover in market-data protocols. The edge case of all-nines reveals whether you think about overflow by default.

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

Source citations

Public interview reports confirming this problem appears in DRW loops.

  • Glassdoor (2025-Q3)DRW SWE candidates report array-manipulation problems including Plus One as short warm-ups in phone screens.
  • Blind (2025-08)DRW interview threads note carry-propagation questions as a test for careful edge-case handling, specifically the all-nines overflow scenario.

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 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]

Explanation: 123 + 1 = 124.

Example 2

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

Explanation: 4321 + 1 = 4322.

Example 3

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

Explanation: 9 + 1 = 10; a new leading digit is prepended.

Approaches

1. Right-to-left carry propagation

Walk the digits array from right to left. If the current digit is less than 9, increment it and return immediately. Otherwise, set it to 0 and continue the carry. If all digits were 9, prepend a 1.

Time
O(n)
Space
O(1) in-place (O(n) only for the all-nines overflow case)
function plusOne(digits) {
  for (let i = digits.length - 1; i >= 0; i--) {
    if (digits[i] < 9) {
      digits[i]++;
      return digits;
    }
    digits[i] = 0; // carry
  }
  // All digits were 9 — overflow
  return [1, ...digits];
}

Tradeoff: O(n) worst case but O(1) average (terminates early). In-place modification with no auxiliary array except for the all-nines overflow case.

DRW-specific tips

DRW interviewers are testing for the all-nines edge case. State it before coding: 'The tricky case is [9,9,9] → [1,0,0,0] — the result array grows by one digit.' After coding, they may ask: 'Generalize to plus k, where k is a multi-digit number' — that is full arbitrary-precision addition. They also ask about sequence-number rollover: 'In a FIX protocol session, sequence numbers are 9-digit integers. What happens at 999999999?' — the same carry-propagation logic applies.

Common mistakes

  • Not handling the all-nines overflow — [9,9,9] should return [1,0,0,0], not [0,0,0].
  • Converting to a JavaScript Number — loses precision for arrays with more than 15 digits (JS Number is 64-bit float).
  • Modifying digits in-place during the loop and not returning early — causes unnecessary iterations.
  • Forgetting to return the modified digits after the increment — easy off-by-one if you break instead of return.

Follow-up questions

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

  • Multiply Strings (LC 43) — arbitrary-precision multiplication without converting to a native integer type.
  • Add Binary (LC 67) — same carry logic in base 2.
  • How would you implement arbitrary-precision addition for two digit arrays, each up to 10^4 digits long?

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 just convert to Number, add 1, and convert back?

JavaScript's Number is a 64-bit float with only 53 bits of integer precision. Arrays longer than 15 digits overflow silently. DRW will explicitly ask you not to convert.

What is the expected number of iterations before the loop terminates?

Expected O(1) if digits are uniformly random (carry propagates past a 9 with probability 0.1 per digit). Worst case O(n) for all-nines.

How does this appear in trading systems?

Sequence numbers in market-data and order-routing protocols are fixed-width integers that roll over on overflow. The carry logic determines whether the rollover is handled cleanly.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →