Execution Timer Wrapper
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L6
Problem
Implement run_timed(fn, args=None). fn is a string of Python lambda source (for example "lambda: 42" or "lambda x, y: x + y") and args is an optional list of positional arguments (default: no arguments). Build the callable from the source string, then wrap it with a timing decorator make_timed that records the start time, calls the function, and measures the elapsed seconds. Invoke the wrapped callable with the supplied positional args and return the original function's return value unchanged. The elapsed time is measured (and may be printed/logged) but is NOT part of the returned value: for "lambda: 42" with no args, return 42; for "lambda x, y: x + y" with args [3, 4], return 7. Handle the no-args case (args is None) by calling the function with zero arguments.
Summary
Function wrapped with a timer. Duration captured on exit.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.