Day 10 of 60 Days of DSA: Add One to The Number (Array-Based Arithmetic)

Incrementing Large Numbers Without Converting to Integers

Thumbnail Image

In coding interviews, you’ll often face problems that look too simple at first glance — and that’s exactly when you solve it, the conditions will sink you down.

One such problem is Add One to a Number, frequently asked in companies like Amazon, Google, Adobe, and Microsoft.
The twist?
You’re not allowed to convert the array into a number. (I know this was the first thought that came into you mind while looking at this problem)

Let’s understand it.

Problem Description

Given a non-negative number represented as an array of digits, add 1 to the number ( increment the number represented by the digits ).
The digits are stored such that the most significant digit is at the head of the list.

NOTE: Certain things are intentionally left unclear in this question which you should practice asking the interviewer. For example: for this problem, the following are some good questions to ask :

Q: Can the input have 0’s before the most significant digit. Or, in other words, is 0 1 2 3 a valid input?
A: For the purpose of this question, YES

Q: Can the output have 0’s before the most significant digit? Or, in other words, is 0 1 2 4 a valid output?
A: For the purpose of this question, NO. Even if the input has zeroes before the most significant digit.

Input/Output

Input 1

Input 1: [1, 2, 3]
Output 1: [1, 2, 4]

Example Explanation

Explanation 1:

Given vector is [1, 2, 3].

The returned vector should be [1, 2, 4] as 123 + 1 = 124.

Why Interviewers Love This Question

This problem checks:

  • Your understanding of carry handling
  • Your ability to work with arrays instead of numbers
  • Edge case awareness (like [9, 9, 9])
  • Whether you ask clarifying questions

Intuitio

Imagine numbers written on paper.

You start adding 1 from the last digit.

Case 1: Last digit is NOT 9

Just add 1 and stop.

[1, 2, 3]

Add 1 → [1, 2,

Case 2: Last digit IS 9

You write 0 and carry 1 to the left, just like school math.

[1, 2, 9]

Becomes → [1, 3, 0]

Case 3: All digits are 9

This creates a new digit at the front.

[9, 9, 9]
+ 1
---------

[1, 0, 0, 0]

Code (In JavaScript)

function addOneToNumber(A){
let n = A.length;
let i = n-1;
let carry = 1;
while(i >= 0 && carry == 1){
if(A[i] < 9){
A[i] +=1;
carry = 0;
}else{
A[i] = 0;
carry = 1;
}
i--;
}
// Handling edge cases where all the number's are 9 and adding 1 to the start of array
if(carry ==1){
A.unshift(1);
}
// to remove initial zeroes
i = 0;
while(A[i] == 0){
A.splice(0, 1);
}
return A;
}

Complexity Analysis

  • Time Complexity: O(n)
    (Single traversal from right to left)
  • Space Complexity: O(1)
    (In-place modification, no extra array used)

Final Thoughts

This problem is not about math — it’s about thinking like a computer.

If you:

  • Handle carry correctly
  • Ask clarifying questions
  • Optimize for large inputs

You’re already thinking like an interviewer expects.

That’s it for Day 10 🎯

You just mastered array-based arithmetic, a pattern that appears frequently in real interview problems.

Tomorrow, we’ll move to another classic DSA pattern 🚀

Do you want to solve more with us?

Then follow this series and share your progress in the comments.

If the topics are difficult for you to understand, then you can refer to the notes on my GitHub.

Here’s the Code link

At Dev Simplified, We Value Your Feedback 📊

👉 Let us know how you’re finding this series so far. It motivates us to write and provide valuable content for you.

👉 Follow us to not miss any updates.

👉 Subscribe for free and join our growing community!