Fix the cost model for a reused MongoDB client

Review the AI's codeEasy10 minAmazon style

Your task

Your Lambda opened a new MongoDB client on every request and closed it at the end. Latency was high, and Atlas showed constant connection churn. The fix creates the client once at module scope, so warm invocations in the same container reuse it. Your team wants a model that prices both versions.

An assistant wrote simulate_connection_cost(is_cold_start, handshake_ms, query_ms, reuse_client). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • is_cold_start: one entry per invocation, in order. true when that invocation runs in a new container.
  • handshake_ms: the time it takes to open a connection (TCP, TLS and auth).
  • query_ms: the time the query itself takes.
  • reuse_client: true for the module scope client, false for a client per request.
  • Return [total_ms, connections_opened] for the whole run.

Rules the tests check:

  • Every invocation pays query_ms.
  • With reuse_client false, every invocation also opens a connection.
  • With reuse_client true, an invocation opens a connection only when its container does not hold one yet. The container then holds it for later invocations.
  • Opening a connection adds handshake_ms to the total and 1 to connections_opened.
  • A cold start is a new container, which holds no connection. The first invocation's container also starts with none, even when it is not marked cold.
  • An empty run returns [0, 0].

Examples

simulate_connection_cost(is_cold_start: list[bool], handshake_ms: int, query_ms: int, reuse_client: bool) -> list[int]
Example 1
Inputsimulate_connection_cost([True, False, False], 120, 10, False)
Output[390, 3]
Example 2
Inputsimulate_connection_cost([True, False, False], 120, 10, True)
Output[150, 1]
Example 3
Inputsimulate_connection_cost([], 120, 10, True)
Output[0, 0]