Fix the heap trend check for OOM triage

Review the AI's codeEasy10 min

Your task

A Java service throws OutOfMemoryError under load. An AI kept answering with a bigger -Xmx, then System.gc() calls, then a different collector, and the errors kept coming back. Your team now classifies the heap from the live set after each full collection before anyone changes a flag. A live set that keeps growing is a leak, and more heap only delays it.

An assistant wrote classify_heap_trend(post_gc_used_mb, heap_max_mb). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • post_gc_used_mb: heap in use right after each full collection, in MB, oldest first.
  • heap_max_mb: the maximum heap size, in MB.
  • Return "insufficient_data", "leak", "undersized" or "healthy".

Rules the tests check, in order:

  1. If heap_max_mb is 0 or less, or there are fewer than 3 samples: "insufficient_data".
  2. If no sample is lower than the one before it, and the last sample is higher than the first: "leak". Samples may stay level for a while.
  3. If the highest sample is at least 85% of heap_max_mb, that is, highest × 100 is at least heap_max_mb × 85: "undersized".
  4. Otherwise: "healthy".

Examples

classify_heap_trend(post_gc_used_mb: list[int], heap_max_mb: int) -> str
Example 1
Inputclassify_heap_trend([900, 1200, 1500, 1800], 4096)
Output"leak"
Example 2
Inputclassify_heap_trend([3700, 3650, 3720, 3690], 4096)
Output"undersized"
Example 3
Inputclassify_heap_trend([400, 380, 410, 395], 4096)
Output"healthy"