Python · Functional Python

Higher Order Functions: Practice Questions

100 questions. Try each one yourself before checking the answer.

Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.

Q1Functions as ValuesEasyMust Do

Show that a function is a value: store it in a variable, put it in a list, and pass it to another function.

Q2lambdaEasy

Write the same small function with def and with lambda, and show they behave identically.

Q3map()EasyMust Do

Use map() to apply a function to every item, and show it returns an iterator.

Q4map()Easy

Use map() with a named function and with a method.

Q5map()Easy

Use map() with two iterables at once.

Q6filter()EasyMust Do

Use filter() to keep only the items that pass a test.

Q7filter()Easy

Use filter(None, ...) to drop every falsy value.

Q8sorted(key=)EasyMust Do

Use sorted(key=…) — the tool that replaces four earlier workarounds.

Q9sorted(key=)Easy

Sort a list of tuples by any field — retiring the Topic 9 trick.

Q10sorted(key=)Easy

Sort a dictionary by its values — retiring the Topic 11 trick.

Q11.sort(key=)Easy

Use .sort(key=…) to sort a list in place, and contrast it with sorted().

Q12max/min with keyEasyMust Do

Use key= with max() and min().

Q13any/allEasy

Use any() to ask whether at least one item passes a test.

Q14any/allEasy

Use all() to ask whether every item passes a test.

Q15any/allEasyMust Do

Combine any() and all() for a validation check.

Q16zip()EasyMust Do

Use zip() to walk two lists together — retiring range(len(...)).

Q17zip()Easy

Use zip() to build a dictionary — retiring the Topic 18 workaround.

Q18zip()Easy

Use zip(*rows) to transpose — retiring the nested comprehension.

Q19enumerate()EasyMust Do

Use enumerate() to get the index and value together.

Q20enumerate()Easy

Combine enumerate() with zip().

Q21reduce()Easy

Use functools.reduce() to fold a sequence into a single value.

Q22reduce()Easy

Use reduce() with a starting value, and show when it genuinely helps.

Q23map() vs ComprehensionEasy

Compare map() with the equivalent comprehension and say which reads better.

Q24filter() vs ComprehensionEasy

Compare filter() with the equivalent comprehension.

Q25Functions as ValuesEasy

Store functions in a dictionary and pick one at runtime — the Topic 11 dispatch table, revisited.

Q26sorted(key=)MediumMust Do

Sort objects by an attribute — retiring the Topic 15 workaround.

Q27operatorMediumMust Do

Use operator.itemgetter and attrgetter instead of writing lambdas.

Q28sorted(key=)MediumMust Do

Sort by several fields in different directions.

Q29sorted(key=)Medium

Sort using a computed key, including a case-insensitive and a "last word" sort.

Q30zip()Medium

Use zip() for calculations across several sequences.

Q31enumerate()Medium

Use enumerate() to find positions and build numbered output.

Q32any/allMedium

Use any() and all() to replace flag-and-loop validation.

Q33functools.partialMedium

Use functools.partial to fix some arguments of a function in advance.

Q34DecoratorsMediumMust Do

Write your first decorator — a function that wraps another function.

Q35DecoratorsMediumMust Do

Use functools.wraps so a decorated function keeps its own name and docstring.

Q36DecoratorsMedium

Write a timing decorator.

Q37DecoratorsMedium

Write a decorator that takes arguments of its own.

Q38lru_cacheMediumMust Do

Use functools.lru_cache — retiring the Topic 11 hand-written memoisation.

Q39map()Medium

Use map() for type conversion across a whole dataset.

Q40ChainingMedium

Chain filter, map and sorted together, then write the same thing as a comprehension.

Q41Functions Returning FunctionsMedium

Write functions that build other functions — the Topic 7 factory, put to work.

Q42sorted(key=)Medium

Sort nested data by a value buried inside it.

Q43reduce()Medium

Use reduce() where it genuinely reads better than a loop.

Q44zip()Medium

Use zip() to compare, pair and rearrange data.

Q45DecoratorsMedium

Write a decorator that validates its function's arguments.

Q46DecoratorsMedium

Write a retry decorator — the Topic 13 pattern, packaged.

Q47any/allMedium

Use any() and all() with zip() to compare collections.

Q48Higher Order FunctionsMedium

Write a function that takes a function as an argument and applies it in different ways.

Q49sorted(key=)Medium

Sort a Counter and a dictionary of lists by derived values.

Q50map() vs ComprehensionMedium

Measure whether map is actually faster than a comprehension.

Q51Real-WorldMediumMust Do

Build a leaderboard with ranks, ties handled and multiple sort criteria.

Q52Real-WorldMedium

Build a small query engine over records using functions as filters.

Q53Real-WorldMedium

Rank word frequencies using sorted(key=) on a Counter.

Q54Real-WorldMediumMust Do

Process a CSV with map, filter and sorted together.

Q55Real-WorldMedium

Group records and sort each group.

Q56Real-WorldMedium

Build a top-N report with ties reported honestly.

Q57any/allMediumMust Do

Validate a batch of records with all() and report every failure.

Q58Real-WorldMediumMust Do

Build a function pipeline that applies stages in order.

Q59Real-WorldMedium

Combine several datasets with zip() and report on them.

Q60Real-WorldMedium

Build a report with enumerate() supplying rank numbers throughout.

Q61DecoratorsMedium

Build a decorator that logs every call to a file.

Q62lru_cacheMediumMust Do

Use lru_cache to speed up a genuinely expensive calculation.

Q63Real-WorldMedium

Sort and filter file data with key functions.

Q64Real-WorldMedium

Build a flexible sorting menu where the user picks the field.

Q65Real-WorldMedium

Analyse survey results with Counter, sorted and any/all.

Q66DecoratorsMedium

Write a decorator that counts how often each function is called.

Q67Real-WorldMedium

Merge and reconcile two datasets using zip, sorted and set operations.

Q68Real-WorldMedium

Build a scoring system where the weights are supplied as functions.

Q69Real-WorldMedium

Apply a chain of transformations chosen at runtime.

Q70Real-WorldMedium

Build a small statistics toolkit where each metric is a function.

Q71Sort StabilityHardMust Do

Show that Python's sort is stable, and use it to sort by two fields with different directions.

Q72sorted(key=)Hard

Show that key= is called exactly once per item, and why that matters.

Q73LazinessHardMust Do

Show that map and filter are lazy, and the bugs that causes.

Q74any/allHard

Show that any() and all() short-circuit, and why that matters.

Q75any/allHard

Explain what any() and all() return for an empty iterable.

Q76zip()Hard

Show that zip() silently stops at the shortest input, and how to avoid losing data.

Q77Late BindingHardMust Do

Show the late-binding trap when lambdas are created inside a loop.

Q78DecoratorsHard

Show what a decorator breaks when it forgets functools.wraps.

Q79reduce()Hard

Show where reduce() becomes unreadable, and what to use instead.

Q80lru_cacheHard

Show the three things that break lru_cache.

Q81sorted(key=)Hard

Show what goes wrong when a key function returns inconsistent types.

Q82map() vs ComprehensionHard

Show the cases where map and filter genuinely lose to a comprehension.

Q83DecoratorsHard

Show what happens when decorators are stacked, and in what order they run.

Q84sorted(key=)Hard

Compare key= with defining __lt__ on a class.

Q85DesignHard

Show a chain of higher-order functions that has become unreadable, and fix it.

Q86Mini-ProjectMini-Project

Build a Leaderboard System with pluggable ranking rules, ties, and multiple views.

Q87Mini-ProjectMini-ProjectMust Do

Build a Query Engine where filters, sorts and projections are all functions.

Q88Mini-ProjectMini-Project

Build a Report Builder where every column is a function of the record.

Q89Mini-ProjectMini-Project

Build a Pipeline Framework that composes stages and reports on each.

Q90Mini-ProjectMini-ProjectMust Do

Build a Decorator Toolkit of five reusable decorators and apply them together.

Q91Mini-ProjectMini-Project

Build a CSV Analyser using every tool in the topic.

Q92Mini-ProjectMini-Project

Build a Text Ranking Engine that scores and ranks documents against a query.

Q93Mini-ProjectMini-Project

Build a Validation Framework where rules are functions and results are reported per field.

Q94Mini-ProjectMini-Project

Build a Caching Layer with statistics, using a decorator you write yourself.

Q95Mini-ProjectMini-Project

Build a Sorting Toolkit demonstrating every key technique in one place.

Q96Higher Order FunctionsInterviewMust Do

Explain what a higher-order function is, and show the four ways they appear in Python.

Q97DesignInterview

Compare map/filter with comprehensions and generator expressions. Give a rule.

Q98sorted(key=)Interview

Explain key= completely: what it does, how it performs, and every pattern worth knowing.

Q99DecoratorsInterview

Explain decorators completely: what they are, how the syntax works, and what to watch for.

Q100CapstoneInterview

Capstone. Build a Higher Order Toolkit Report using every tool in the topic: map, filter, reduce, sorted(key=), max/min with key, any, all, zip, enumerate, itemgetter, partial, lru_cache and a decorator of your own.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session