‹ DS&A interview · Socratic
DSA · Arrays & Math · #137

Plus One

Module 79 · difficulty 2/5·30:00starts on first keystroke

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 integer does not contain any leading zero, except the number `0` itself. Implement `plusOne(digits)` which increments the large integer by one and returns the resulting array of digits.

Examples
  • digits = [1,2,3] [1,2,4]The integer 123 plus one is 124.
  • digits = [4,3,2,1] [4,3,2,2]The integer 4321 plus one is 4322.
  • digits = [9] [1,0]9 plus one is 10, which needs an extra digit.
Constraints
  • · 1 <= digits.length <= 100
  • · 0 <= digits[i] <= 9
  • · digits has no leading zeros except for the number 0 itself.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.