L2 · Working engineerPython internals~4 min · 4 tests#56

map, filter and reduce in Python

Use map(), filter() and functools.reduce() together: double every number, keep the evens and multiply them all. Functional Python with live tests.

The problem

Return a tuple of three results computed from nums:

  1. every number doubled — using map
  2. only the even numbers — using filter
  3. the product of all numbers — using functools.reduce

For an empty list, the product is 1.

Examples

  1. Example 1

    Input

    functional([1, 2, 3, 4, 5])

    Expected output

    ([2, 4, 6, 8, 10], [2, 4], 120)
  2. Example 2

    Input

    functional([3])

    Expected output

    ([6], [], 3)

+ 2 hidden tests on Submit — reduce needs an initial value.

Edge cases to ask about

  • Empty list (reduce initial value)
  • Zero in the product

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · functional
    ⌘/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
    map / filter / reduceO(n)O(n)map and filter are lazy iterators — wrap in list().
    Walkthrough of the optimal approach (try it yourself first)

    map and filter return lazy iterators in Python 3, so wrap them in list(). reduce moved to functools and needs an initial value (1 for a product) or it raises TypeError on an empty list — that is what the hidden test checks.

    Most Python style guides prefer comprehensions over map/filter with lambdas; say so.

    Complexity: O(n) time, O(n) space. Each function makes one pass; the two lists can hold n items.

    Reveal the reference solution
    from functools import reduce
    
    def functional(nums):
        doubled = list(map(lambda x: x * 2, nums))
        evens = list(filter(lambda x: x % 2 == 0, nums))
        product = reduce(lambda a, b: a * b, nums, 1)
        return doubled, evens, product

    Follow-ups interviewers ask

    • Rewrite all three with comprehensions and math.prod.

    Frequently asked interview questions

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

    What is the time complexity of map, filter and reduce in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. Each function makes one pass; the two lists can hold n items.

    What follow-up questions do interviewers ask about map, filter and reduce in Python?

    Rewrite all three with comprehensions and math.prod.