L2 · Working engineerPython internals~3 min · 5 tests#57

*args and **kwargs Explained

Write a Python function that accepts any positional and keyword arguments with *args and **kwargs and sums each. Learn packing and unpacking live.

The problem

Write sum_args(*args, **kwargs) that returns a tuple (sum of positional arguments, sum of keyword-argument values).

sum_args(10, 20, 30, a=5, b=10) → (60, 15).

Examples

  1. Example 1

    Input

    sum_args(10, 20, 30, a=5, b=10)

    Expected output

    (60, 15)
  2. Example 2

    Input

    sum_args(1, 2)

    Expected output

    (3, 0)

+ 3 hidden tests on Submit — no arguments, unpacking at the call site.

Edge cases to ask about

  • No arguments
  • Only keywords
  • Unpacking at the call

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · sum_args
    ⌘/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 read why.

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

    Pick both to reveal the answer.

    From brute force to optimal

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

    ApproachTimeSpaceIdea
    PackingO(n)O(n)args is a tuple, kwargs is a dict.
    Walkthrough of the optimal approach (try it yourself first)

    *args packs extra positional arguments into a tuple; **kwargs packs keyword arguments into a dict. At a call site the same symbols unpack: f(*my_list, **my_dict).

    Decorators (questions 58, 68, 84) rely on exactly this to forward any signature.

    Complexity: O(n) time, O(n) space. Python packs the n arguments into a tuple and a dict; summing them is linear.

    Reveal the reference solution
    def sum_args(*args, **kwargs):
        return sum(args), sum(kwargs.values())

    Follow-ups interviewers ask

    • What is a keyword-only argument (def f(a, *, b))?
    • Positional-only (/)?

    Frequently asked interview questions

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

    What is the time complexity of *args and **kwargs Explained in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. Python packs the n arguments into a tuple and a dict; summing them is linear.

    What follow-up questions do interviewers ask about *args and **kwargs Explained?

    What is a keyword-only argument (def f(a, *, b))? Positional-only (/)?