Impure Function That Logs to a Shared List

Easy ⏱ 8 min 77% acceptance ★★★★☆ 4.3
A module-level list audit_log = [] already exists. Write a function record_event(event) that appends event to audit_log (an intentional side effect, making this function impure) and returns the new length of audit_log. This problem is about recognizing and correctly implementing an impure function, contrasted with pure functions elsewhere.

Examples

Example 1
Input
record_event("login")
record_event("logout")
Output
1, 2
Explanation

Each call mutates the shared audit_log list and returns its new size.

Constraints

  • audit_log is defined once at module level and shared across calls.
  • The function must mutate audit_log in place (append), not reassign it to a new list.

Topics

Functionspure vs impure functions

Companies

IBMHCLTech

Hints

Hint 1

list.append() mutates in place, so you do not need the global keyword just to call it (global is only required to rebind the name itself).

Hint 2

Return len(audit_log) after appending.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.