Plus One
Updated
Problem statement
You are given a non-negative integer as an array digits. Each element is one digit from 0 to 9. The digits go from the most significant digit (first) to the least significant digit (last).
Add one to the integer and return the digits of the result in the same form.
The array has no leading zeros, except when the integer is 0 itself, which is given as [0].
Function
plusOne(digits: int[]) → int[]Examples
Example 1
digits = [1,2,3]return = [1,2,4]The array stands for 123. The last digit 3 becomes 4, and nothing is carried, so the answer is [1,2,4].
Example 2
digits = [1,9,9]return = [2,0,0]The array stands for 199, and 199 + 1 = 200. Both trailing 9s become 0, and the carry turns the leading 1 into 2.
Example 3
digits = [9,9,9]return = [1,0,0,0]The array stands for 999, and 999 + 1 = 1000. Every digit becomes 0, so a new leading 1 is added and the answer has four digits.
Constraints
1 <= digits.length <= 10000 <= digits[i] <= 9digits[0] != 0unlessdigitsis[0].
More Druva Onsite Interview questions
Similar questions at other companies
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Add One to a Number Represented as DigitsOracle · ONSITE INTERVIEWOracle interview questions
- Streaming Entropy, Part 1: Batch EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Majority ElementMicrosoft · ONSITE INTERVIEWMicrosoft interview questions