Lambdas & Higher-Order Functions Interview Questions & Answers

5 questions Updated 2026-06-18

Python interview questions on lambda syntax and limitations, lambda vs def, higher-order functions, the key argument in sorted/max/min, and functions as first-class objects.

A lambda is an anonymous, single-expression function: lambda args: expression. It returns the expression's value automatically (no return). Its limitation is exactly that — it can hold only one expression, no statements, assignments, loops, or annotations.

square = lambda x: x * x
square(5)                  # 25

add = lambda a, b=1: a + b # defaults allowed
add(10)                    # 11

# NOT allowed: lambda x: (y = x; return y)  — no statements

Because it's an expression, a lambda can be passed inline wherever a function is expected. Keep them short; anything needing multiple lines or a docstring should be a named def.

Use a lambda for a tiny throwaway function passed inline as an argument (a sort key, a callback). Use def for anything reused, named, documented, or non-trivial. A named function gives a useful __name__ in tracebacks; a lambda just shows <lambda>.

# good lambda: inline, one-off
sorted(words, key=lambda w: len(w))

# prefer def: reused / needs a name
def by_length(w):
    return len(w)

PEP 8 even discourages assigning a lambda to a name (f = lambda x: ...) — if you need a name, just use def. Lambdas shine as arguments, not as definitions.

A higher-order function is one that takes a function as an argument and/or returns a function. They enable composing and parameterizing behavior. Built-in examples include map, filter, sorted, and the functools tools.

def apply_twice(fn, x):
    return fn(fn(x))           # takes a function

apply_twice(lambda n: n + 3, 10)   # 16

list(map(str.upper, ["a", "b"]))   # ['A', 'B']
list(filter(lambda n: n > 0, [-1, 2, -3, 4]))  # [2, 4]

Higher-order functions are the foundation of functional-style Python and of decorators (which both take and return functions). They let you pass behavior around as data.

key takes a function applied to each element to derive the value used for comparison — the elements themselves aren't changed, only how they're ranked. It's used by sorted, list.sort, max, and min.

words = ["banana", "kiwi", "apple"]

sorted(words, key=len)                 # ['kiwi', 'apple', 'banana']
max(words, key=len)                    # 'banana'
sorted(words, key=str.lower)           # case-insensitive

people = [("ada", 36), ("grace", 45)]
max(people, key=lambda p: p[1])        # ('grace', 45)

key is called once per element (efficient), unlike the old cmp style. For multi-level sorts, return a tuple: key=lambda p: (p.last, p.first).

In Python, functions are first-class objects: they can be assigned to variables, stored in data structures, passed as arguments, and returned from other functions — just like any value. This is what makes higher-order functions and closures possible.

def shout(s): return s.upper() + "!"

f = shout                 # assign to a variable
f("hi")                   # 'HI!'

dispatch = {"loud": shout}        # store in a dict
dispatch["loud"]("hey")           # 'HEY!'

def make_op(op):                  # return a function
    return (lambda a, b: a + b) if op == "+" else (lambda a, b: a - b)
make_op("+")(2, 3)                # 5

Treating functions as values enables strategy/dispatch tables, callbacks, and decorators. There's no separate "function pointer" concept — the function is the object.

Practice tests are coming soon

Get notified when interactive mock interviews and quizzes launch.