Add Two Numbers
Real-world version: Odometer Tape SumThe statement here is our own: the same inputs, outputs and technique.
A workshop logs mileage counters onto punched paper tapes. A tape is a one-way chain of cells: every cell holds a single digit and links to the cell punched after it, and the final cell links to nothing. The punch head starts at the smallest place value, so the front cell of a tape carries the ones digit, the cell after it carries the tens digit, and so on.
Because the harness passes plain JSON, each tape arrives as an array of its digits in punch order, ones digit at index 0. You get two of them, left and right. Neither tape is empty, and neither carries useless high-order padding: the last cell of a tape is nonzero unless the whole tape is a single 0.
Return the tape that records the sum of the two counter values, laid out the same way: an array of digits with the ones digit first. Produce it cell by cell while stepping through both chains together and pushing any overflow into the next place. Do not fold either chain into one big number before adding.
Examples
Example 1
- Input
- left = [6, 8, 4], right = [7, 3]
- Output
- [3, 2, 5]
The tapes record 486 and 37. Adding the ones cells gives 13, so the answer's front cell is 3 and 1 rides into the tens place, where 8 + 3 + 1 = 12 leaves 2 and another rider, and 4 + 1 = 5 finishes the hundreds place.
Example 2
- Input
- left = [5, 9, 9], right = [8, 1]
- Output
- [3, 1, 0, 1]
995 plus 18 is 1013. The overflow survives past the end of both tapes, so the answer needs one more cell than the longer input.
Example 3
- Input
- left = [0], right = [4, 7]
- Output
- [4, 7]
A counter sitting at zero is punched as a single 0 cell, and adding it leaves 74 untouched.
Constraints
- 1 <= left.length <= 100
- 1 <= right.length <= 100
- 0 <= left[i] <= 9
- 0 <= right[i] <= 9
- Neither tape has high-order padding: its last digit is nonzero unless the tape is exactly [0]
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def sum_odometer_tapes(left: list[int], right: list[int]) -> list[int]:public int[] sumOdometerTapes(int[] left, int[] right)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 8 test cases: 8 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's