Time a function's execution

Python data engineering interview problem. Difficulty: beginner. Pattern: OOP. About 10 minutes. Free to practice.

A job wrapper should record that a function ran (and optionally how long) so a log line exists even when the function itself prints nothing useful.

Support wants a log line for every call to a helper: its name, the arguments, and the result. Return a dictionary with the function name, the args as a list, and the result, which is the sum of the arguments. An empty argument list has a result of 0. Write call_with_log(name, args). call_with_log('sum', [1, 2, 3]) returns {'name': 'sum', 'args': [1, 2, 3], 'result': 6}. With no arguments it returns {'name': 'empty', 'args': [], 'result': 0}. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Return a dictionary with name, args and result.
  • result is the sum of the arguments.
  • args is a list.
  • An empty list gives result 0.

Constraints

  • args is a list of numbers.
  • The result must not change the input list.

Examples

Input: call_with_log('sum', [1, 2, 3]) Output: {'name': 'sum', 'args': [1, 2, 3], 'result': 6} This input follows the stated rules and produces this output.

Topics: lakebench, python, decorators.

More Python interview questions · All interview problems · Learn data engineering

beginner

Time a function's execution

Interview-style drill: Write a decorator that measures and reports how long a wrapped function takes to run.

Call a function with logging around it as the tests specify.