All Told

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

Domain
Python
Difficulty
easy
Seniority
L3

Problem

Each value in counts is the number of events one server logged during a shift, and there is always at least one server. Return the fleet's combined total.

Every shift leaves a number behind. Total the fleet.

Example 1

Input:

counts = [1,2,3,4,5]

Output:

15

Example 2

Input:

counts = [10]

Output:

10

Worked solution and explanation

What's really being tested

This is the warm-up they use to clock your instincts, not your algorithm chops. Summing a list of shift counts is the simplest accumulation there is, so nobody fails on the arithmetic. What they actually watch is where you put the return: drop it one indent too deep and you hand back the first server's count and call it the fleet total. On a five-line problem, that misplaced indent is the only thing standing between a clean pass and an awkward restart.


The two decisions

Step 1: Start the total at zero

Zero is the identity for addition, so an empty-looking accumulator can't poison the result. Start it at the wrong value and every answer is off by a constant nobody notices until the totals look subtly wrong in prod.

Step 2: Fold each value in

Walk the list once and add each count to the running total. One pass, every element touched exactly once: there is no reason to index by position or build a second list.


The solution

Linear accumulation with a running total
def sum_list(counts):
    total = 0
    for value in counts:
        total += value
    return total

Common follow-up questions