Carry the One

A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
medium
Seniority
mid
Asked in
Snap

Problem

We store counters too big for 64 bits as lists of chunks. Each chunk is a number from 0 to 999,999,999 and works as one digit in base 1,000,000,000, with index 0 the lowest chunk. Subtract b from a (a is never smaller than b) the way you would on paper, column by column. Return the indices of the columns that have to borrow from the chunk above, lowest first.

In grade-school subtraction you scribble a mark each time you borrow. Recover those marks.

Example 1

Input:

a = [0,1], b = [1]

Output:

[0]

Example 2

Input:

a = [0,0,1], b = [1]

Output:

[0,1]

Worked solution and explanation

Why this problem exists in real interviews

This is grade-school borrow propagation hiding behind a big-number storage format. Each list entry is one chunk (bignum libraries call it a limb). When you subtract b from a column by column, a column can be forced to borrow from the one above it, and that can force the next column to borrow too. The interviewer is watching whether you can track that ripple. The obvious move is to fold both lists into native Python ints and subtract, but a - b only gives you the difference, and its digits do not record which columns borrowed to produce it. The only way to know where a borrow happened is to redo the subtraction one column at a time, carrying a single borrow flag from index 0 upward.


Break down the requirements

Step 1: Line up the columns, treating a shorter b as zeros

Because a is never smaller than b, a has at least as many chunks. Walk the indices of a and read b[i] when it exists, treating any missing high chunk of b as 0. That one guard handles a shorter b without building a padded copy.

Step 2: Carry one borrow flag across the columns

At index i the column must cover b[i] plus whatever borrow arrived from below. If a[i] - borrow - chunk_b goes negative, this column borrows: record i and set borrow to 1 for the next column up; otherwise reset it to 0. That single flag is what lets a borrow ripple through a run of zero chunks.

Step 3: Collect the indices in order

Walking from index 0 upward records the indices lowest first already, so return them as they are. When no column borrows, the list stays empty.


The solution

One linear pass with a carried borrow
def borrow_positions(a, b):
    positions = []
    borrow = 0
    for i in range(len(a)):
        chunk_b = b[i] if i < len(b) else 0
        if a[i] - borrow - chunk_b < 0:
            positions.append(i)
            borrow = 1
        else:
            borrow = 0
    return positions
Independent per-column test
Flag column i whenever a[i] < b[i], judging each column alone. It reads clean but ignores the borrow flowing in from below, so it misses every column that borrows only because of a ripple. Subtracting [1] from [0, 0, 1] flags 1 column where the answer has 2.
Carried borrow flag
Carry one borrow forward as you scan, so column i sees the shortfall created beneath it. It is O(n), compares only small fixed-size numbers, and marks borrows exactly the way paper subtraction and production bignum code do.

Common follow-up questions