L1 · FoundationsList basics~1 min · 4 tests

Sum a List Without sum()

Add up a Python list with a loop and an accumulator instead of sum(), then compare with the built-in. O(n) basics with live test cases in your browser.

The problem

Return the total of all numbers in nums without using sum().

Examples

  1. Example 1

    Input

    list_total([1, 2, 3, 4])

    Expected output

    10
  2. Example 2

    Input

    list_total([-1, 1])

    Expected output

    0

+ 2 hidden tests on Submit.

Edge cases to ask about

  • Empty list
  • Floats
  • Negatives

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · list_total
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Accumulator loopO(n)O(1)
    Walkthrough of the optimal approach (try it yourself first)

    An accumulator starting at 0, updated once per element. The built-in sum() does the same in C, so it's faster — but the interviewer wants to see the loop.

    Complexity: O(n) time, O(1) space. One addition per element.

    Reveal the reference solution
    def list_total(nums):
        total = 0
        for x in nums:
            total += x
        return total

    Follow-ups interviewers ask

    • Sum only the even-indexed elements.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Sum a List Without sum() in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. One addition per element.

    What follow-up questions do interviewers ask about Sum a List Without sum()?

    Sum only the even-indexed elements.