Quick Overview

Add one to an unsigned integer stored as a fixed-size decimal digit array without converting the value or using a dynamically appended collection, including the all-nines overflow case.

Add One to an Integer Stored as Digits Without ArrayList

Company: Oracle

Role: Backend Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem An unsigned integer is represented by a fixed-size array of decimal digits in most-significant-first order. Add one and return the resulting digit array. Do not convert the entire input to an integer, string, linked collection, or dynamically appended list. You may modify the input array. If every digit is `9`, allocate one new fixed-size array of length `n + 1`; otherwise return the updated original array. ### Function Contract Implement `plusOneFixedDigits(digits)`. ### Constraints & Assumptions - `1 <= len(digits) <= 200,000`. - Every element is an integer from `0` through `9`. - The representation has no leading zero unless the number itself is zero. - The restriction against a dynamic list is part of the exercise. ### Clarifying Questions to Ask - Is input mutation allowed? Yes. - Can the output need one extra digit? Yes, only when the input is all nines. - Should `[0]` become `[1]`? Yes. ```hint Propagate from the least significant digit Scan right to left. A digit below nine can absorb the carry and lets you return immediately; a nine becomes zero and keeps the carry alive. ``` ### Examples ```text [1,2,3] -> [1,2,4] [1,2,9] -> [1,3,0] [9,9,9] -> [1,0,0,0] [0] -> [1] ``` ### Evaluation Focus - Handles carry propagation and the all-nines expansion. - Does not use whole-number conversion or dynamic append operations. - Runs in `O(n)` worst-case time and uses `O(1)` extra space unless expansion is required. ### Extensions to Discuss 1. How would you add an arbitrary nonnegative integer instead of one? 2. How would little-endian digit order simplify or complicate the loop? 3. What changes for a singly linked list representation?

Overview: Add one to an unsigned integer stored as a fixed-size decimal digit array without converting the value or using a dynamically appended collection, including the all-nines overflow case.

Read the full Oracle Backend Engineer interview experience this question came from

A nonnegative integer is stored as a nonempty most-significant-first decimal digit array. Add one without converting the full value. Mutate and return the input unless all digits are nine; only then allocate a new length n+1 result.

Constraints

  • 1 <= len(digits) <= 200000.
  • Every digit is between 0 and 9.
  • No leading zero exists except for [0].
  • Whole-number conversion and dynamic append operations are forbidden.

Examples

Input: ([1, 2, 3],)

Expected Output: [1, 2, 4]

Explanation: No carry propagation is needed.

Input: ([9, 9, 9],)

Expected Output: [1, 0, 0, 0]

Explanation: All nines require a longer result.

Hints

  1. Scan from least significant to most significant.
  2. Only an all-nines input needs a longer array.

Loading coding console...