Fix the claim step for overlapping notification jobs

Review the AI's codeEasy10 minStripe style

Your task

A cron job sends unsent notifications every minute. When one run is slow, the next run starts before it ends, and both select the same rows and send them twice. The fix is a claim step: each notification goes to one worker, and each worker takes at most one batch.

An assistant wrote claim_notifications(claim_attempts, batch_limit). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • claim_attempts: claim attempts in the order the database sees them, each meant to be "worker_id|notification_id".
  • batch_limit: the most notifications one worker may hold, an integer. It can be 0.
  • Return the granted claims as "worker_id|notification_id" strings, in the order they were granted.

Rules the tests check:

  • Skip an attempt unless it splits on | into exactly 2 parts and neither part is empty.
  • Refuse an attempt for a notification that was already granted, even to the same worker.
  • Refuse an attempt from a worker that already holds batch_limit notifications. The notification stays unclaimed, so a later attempt from another worker can still get it.
  • Otherwise grant the attempt: the worker now holds that notification.

Examples

claim_notifications(claim_attempts: list[str], batch_limit: int) -> list[str]
Example 1
Inputclaim_notifications(["w1|n1", "w2|n1", "w1|n2"], 5)
Output["w1|n1", "w1|n2"]
Example 2
Inputclaim_notifications(["w1|n1", "w1|n2", "w1|n3", "w2|n4"], 2)
Output["w1|n1", "w1|n2", "w2|n4"]