### How to prepare for your next Coding Interview

Here are some tips while preparing for your coding interviews. 1. Do study or…

September 03, 2020

Given a non-empty array of digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

**Example**

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

It has very simple solution, just by iterating array from the end. And, keeping track of carry.

A simple complexity here is that result can be bigger than original array if we got a carry for the last sum.

```
public int[] plusOne(int[] digits) {
int l = digits.length;
//initializing carry with the number we want to add for first time.
int carry = 1;
for (int i=l-1; i>=0; i--) {
digits[i] = digits[i] + carry;
carry = digits[i]/10;
digits[i] = digits[i]%10;
}
// copy result to another array
int targetSize = carry == 1 ? l+1 : l;
int[] res = new int[targetSize];
int i=0;
if (carry == 1) {
res[0] = carry;
i = 1;
}
for (; i<targetSize; i++) {
res[i] = digits[i-carry];
}
return res;
}
```

Its `O(n)`

Here are some tips while preparing for your coding interviews. 1. Do study or…

Problem Statement You are given an n x n 2D matrix representing an image, rotate…

Problem Statement Write a function to find the longest common prefix string…

Problem Statement Given a linked list, remove the n-th node from the end of list…

Problem Statement Maximum Length of Subarray With Positive Product. Given an…

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

Problem Statement You are given a string text of words that are placed among…

Problem Statement You are given a rows x cols matrix grid. Initially, you are…

Problem Statement Given a string s, return the maximum number of unique…

Problem The Leetcode file system keeps a log each time some user performs a…

Problem Statement Replace all spaces in a string with ‘%20’ (three characters…

Problem Implement an algorithm to determine if a string has all the characters…