Fix the leak count for a Lambda audit trail
Your task
Your AWS Lambda handler builds an audit trail and returns it to the caller. The trail array sits at module scope, outside the handler. A warm container (one that Lambda keeps and reuses for later requests) keeps that array between invocations, so a caller can see entries from other users. You replay a stream of invocations on one warm container to measure that leak.
An assistant wrote count_leaked_trail_entries(user_ids, module_scope). It is in the editor. It has a bug. Find it and fix it.
Input and output:
user_ids: the user id of the caller of each invocation, in arrival order. All of them land on the same warm container. An id is a string. It can repeat and it can be empty.module_scope: true when the trail is the module scope array that survives between invocations. False when the handler builds a fresh trail on every invocation.- Return a list of integers, one per invocation, in the same order: how many entries in the trail returned to that caller belong to a different user.
Rules the tests check:
- Each invocation adds its own entry to the trail first, then returns the trail.
- With
module_scopefalse, the returned trail holds only the caller's own entry, so every count is0. - With
module_scopetrue, the trail returned by invocationiholds the entries of invocations0toi. - An entry belongs to a different user when its user id is not exactly equal to the caller's id. Case matters:
Aandaare different users. The empty string""is an id like any other. - An empty
user_idsreturns an empty list.
Examples
count_leaked_trail_entries(user_ids: list[str], module_scope: bool) -> list[int]Example 1
Input
count_leaked_trail_entries(["u1", "u2", "u3"], True)Output
[0, 1, 2]Example 2
Input
count_leaked_trail_entries(["u1", "u2", "u3"], False)Output
[0, 0, 0]Example 3
Input
count_leaked_trail_entries([], True)Output
[]