Fix the cold start wait in a Lambda handler

Review the AI's codeEasy10 minAmazon style

Your task

Your Lambda started connecting its API client at module load and never waited for it. A request that arrived before the connection finished crashed with client is undefined, mostly right after a deploy. The fix creates one promise for the client at module load, and every request awaits it before it uses the client. You model when each request finishes with that fix in place.

An assistant wrote handler_completion_times(arrival_times, init_ready_time, handler_ms). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • arrival_times: when each request arrives, in milliseconds, as integers. They are not sorted and can repeat.
  • init_ready_time: when the client becomes ready, in milliseconds. An integer, 0 or more.
  • handler_ms: how long the handler runs once it starts, in milliseconds. An integer, 0 or more.
  • Return a list of integers, one per request, in input order: the time each request finishes.

Rules the tests check:

  • A request starts at its arrival time or at init_ready_time, whichever is later. No request starts before the client is ready.
  • A request finishes handler_ms after it starts.
  • Requests do not wait for each other. Each one is worked out on its own.
  • Keep the input order. Do not sort and do not remove repeats.
  • An empty arrival_times returns an empty list.

Examples

handler_completion_times(arrival_times: list[int], init_ready_time: int, handler_ms: int) -> list[int]
Example 1
Inputhandler_completion_times([0, 5, 100], 40, 10)
Output[50, 50, 110]
Example 2
Inputhandler_completion_times([7, 3, 9], 5, 0)
Output[7, 5, 9]