Add one to a non-negative integer stored as an array of decimal digits

Quick Overview

A technical exercise for product manager candidates: write code that adds one to a non-negative number stored as an array of single decimal digits. It tests carry handling, edge cases where the result grows longer, complexity analysis, testing habits and the ability to explain code clearly.

Add one to a non-negative integer stored as an array of decimal digits

Company: Google

Role: Product Manager

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

You are given an array in which every element is a single decimal digit between 0 and 9, and the elements together represent one non-negative whole number. Write a function that returns the digits of that number after one has been added to it. For example, if the digits represent 129, the result should represent 130. Talk through your approach as you code, then explain how you would test it. ```hint Do it by hand first Work out 129 + 1, 199 + 1 and 999 + 1 on paper before writing any code. Notice which digits change each time, and what is different about the last case. ``` ### Clarifying Questions - Does the most significant digit come first, so that `[1, 2, 9]` means 129, or is the array stored with the ones digit first? - Can the input be empty or contain leading zeros, such as `[0, 0, 7]`, and if so what should the output look like? - Should the function update the caller's array in place or return a new one, and is it acceptable for the result to be longer than the input? - Is every element guaranteed to be an integer from 0 to 9, or should the function reject invalid input? ### What a Strong Answer Covers - A precise statement of how the array encodes the number and what the function returns, agreed before coding. - Correct carry handling, including the case where every digit is 9 and the result gains a digit. - Time and extra-space cost, and a comparison with the alternative of converting the whole array to a built-in integer and back. - A small, deliberate set of test cases that covers the boundaries. - A clear explanation that a non-specialist listener could follow. ### Follow-up Questions - How would the code change if the digits were stored ones digit first, or held in a singly linked list instead of an array? - Generalize the function to add any non-negative integer k to the digit array. - How would you adapt it to subtract one, and what should happen when the input is zero?

Overview: A technical exercise for product manager candidates: write code that adds one to a non-negative number stored as an array of single decimal digits. It tests carry handling, edge cases where the result grows longer, complexity analysis, testing habits and the ability to explain code clearly.

Google logo
Google
May 13, 2025
mediumProduct ManagerOtherSoftware Engineering Fundamentals
0
0

You are given an array in which every element is a single decimal digit between 0 and 9, and the elements together represent one non-negative whole number. Write a function that returns the digits of that number after one has been added to it. For example, if the digits represent 129, the result should represent 130. Talk through your approach as you code, then explain how you would test it.

Clarifying Questions Guidance

  • Does the most significant digit come first, so that [1, 2, 9] means 129, or is the array stored with the ones digit first?
  • Can the input be empty or contain leading zeros, such as [0, 0, 7] , and if so what should the output look like?
  • Should the function update the caller's array in place or return a new one, and is it acceptable for the result to be longer than the input?
  • Is every element guaranteed to be an integer from 0 to 9, or should the function reject invalid input?

What a Strong Answer Covers Guidance

  • A precise statement of how the array encodes the number and what the function returns, agreed before coding.
  • Correct carry handling, including the case where every digit is 9 and the result gains a digit.
  • Time and extra-space cost, and a comparison with the alternative of converting the whole array to a built-in integer and back.
  • A small, deliberate set of test cases that covers the boundaries.
  • A clear explanation that a non-specialist listener could follow.

Follow-up Questions Guidance

  • How would the code change if the digits were stored ones digit first, or held in a singly linked list instead of an array?
  • Generalize the function to add any non-negative integer k to the digit array.
  • How would you adapt it to subtract one, and what should happen when the input is zero?