THINK FIRST·CODE LATER

← All labs

Serverless cold starts and keep-alive

Problem

Simulate a serverless platform where each instance handles one request at a time.

Input: keepAlive coldMs execMs, then request arrival times (ms, non-decreasing).

For each request at time t:

  1. Examine existing instances in creation order. An instance that is free (busy until ≤ t) but whose last finish time is more than keepAlive before t has expired: add keepAlive to its idle time and retire it.
  2. Among the remaining free instances, choose the one with the latest last finish time (most recently used; ties → lowest index). Add (t − last finish) to its idle time; the request runs warm: latency = execMs.
  3. If none, start a new instance: cold start, latency = coldMs + execMs.

Print t=900: warm on I3, latency 100 ms or t=0: COLD start I1, latency 600 ms. At the end, add keepAlive of final idle time to each instance not retired, and print Requests: n, cold starts: c (p%), average latency X ms (one decimal) and Instances: k, idle warm time billed: T ms.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.