KatInterview

Batched Notification Sender with Cooldown-Aware Retries

Heaps, Sorting & SearchModel answer
MediumModel answerHeaps, Sorting & Searchbatchingpriority-queueretry-scheduling

Last updated

Push messages through a provider that accepts at most 5 per call, and retry each failure only after its own cooldown expires, with an attempt cap and no busy-waiting.

You are building the delivery layer for a notification service. All messages go out through an external provider with two rules:

  • A single call accepts at most 5 messages.
  • For each message in the call, the provider answers (True, payload) on success, or (False, cooldown_seconds) on failure. The cooldown is how long you must wait, counted from when you received that response, before that particular message may be sent again.

Responses come back in the same order as the messages in the call.

Part 1: Plain batching

Write send_all(messages, send_batch). It must deliver every message using the minimum number of calls allowed by the size limit. Nothing may be skipped or sent twice, and the final, smaller group must be handled. Return one outcome per message, in input order.

def send_all(messages: list, send_batch) -> list:
    """send_batch takes a list of up to 5 messages and returns one
    (ok: bool, payload) pair per message, in the same order."""

Part 2: Retries with per-message cooldowns

Write send_with_retries(messages, send_batch, max_attempts=5). Failed messages are retried once their own cooldown has passed. Requirements:

  • A message in cooldown must not hold up messages that are ready.
  • Ready messages keep flowing in batches of up to 5.
  • When nothing is ready, the program must not spin on the clock.
  • max_attempts counts every send of a message, including the first. A message that is still failing when it reaches the cap is reported as a permanent failure, as (False, last_payload).
def send_with_retries(messages: list, send_batch, max_attempts: int = 5) -> list:
    ...

Example

Take a fake provider where message i fails its first i % 3 attempts, each time reporting a 0.01 s cooldown, and then succeeds.

messages = list(range(12))
# Every message ends as (True, "ok:<i>"), after 1, 2 or 3 attempts depending on i % 3.

Constraints

  • Up to 10^4 messages.
  • Cooldowns are non-negative numbers of seconds. They are tiny in tests but the code must work unchanged for multi-second values.
  • Return results in the original input order.

What to cover in your answer

Explain your data structures, how you avoid busy-waiting, the complexity, and the edge cases. Also say which clarifying questions you would ask, for example whether calls may run concurrently.

Hints

Hint 1

Part 1 is just slicing the list in steps of 5. The only thing to get right is writing each response back to the original position, using the slice's starting offset.

Hint 2

Keep two collections in Part 2: a queue of messages that can be sent now, and a min-heap keyed by the time each cooling-down message becomes eligible. The heap's top always tells you the earliest wake-up time.

Hint 3

When the ready queue is empty, sleep for exactly (earliest ready time - current time), then move every due entry from the heap to the ready queue. Take the cooldown timestamp right after the response arrives, and use a per-message attempt counter to enforce the cap.