The Map Reducer
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L3
Problem
Given a list of `values`, a `mapper`, and a `reducer`, return the single value produced by applying `mapper` to every element and then reducing the mapped results left-to-right with `reducer`. The catch: `mapper` and `reducer` arrive as SOURCE STRINGS of lambda expressions (for example the string "lambda x: x * 2" and the string "lambda a, b: a + b"), so you must `eval` each string into a real callable before you can call it. `mapper` takes one argument and returns a transformed value; `reducer` takes two arguments (accumulator, next) and returns one combined value, applied cumulatively from left to right like `functools.reduce`. Return the final reduced value. If `values` is empty, return `None`.
Summary
Map it. Reduce it. One answer.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.