FrontendInterviews.dev

Loading problem…

52. Notification Scheduler with Cooldown Periods

Medium•

You're building a notification system for a social media app. Users can receive different types of notifications (like "like", "comment", "follow", etc.), but to avoid overwhelming users, you need to enforce a cooldown period between notifications of the same type.

Given an array of notification types (each represented by a letter from A to Z) and a cooldown period n, determine the minimum number of time slots needed to schedule all notifications. Each time slot can either:

  • Send one notification, or
  • Be idle (waiting for cooldown to expire)

Constraint: There must be at least n time slots between two notifications of the same type.

Requirements

1. Basic Functionality

  • Accept an array of notification types (strings) and a cooldown period n
  • Return the minimum number of time slots required to schedule all notifications
  • Enforce cooldown period between same-type notifications

Example Usage

minTimeSlots(['A', 'A', 'B', 'A'], 2);  // 7 - A _ _ A B _ A
minTimeSlots(['A', 'B', 'C'], 0);       // 3 - no cooldown idle slots needed
minTimeSlots(['A', 'A', 'A'], 2);       // 7 - A _ _ A _ _ A

Real-World Context

This problem models real notification systems where:

  • Email notifications: Prevent sending too many emails of the same type to users
  • Push notifications: Avoid notification fatigue by spacing out similar alerts
  • In-app notifications: Manage notification queue with type-based rate limiting
  • Task scheduling: Schedule background jobs with cooldown periods between same-type tasks

Constraints

  • 1 <= notifications.length <= 10^4
  • Each notification type is a single uppercase letter (A-Z)
  • 0 <= cooldown <= 100
  • You can schedule notifications in any order
  • Idle slots can be inserted anywhere to satisfy cooldown requirements
Accepted25/38|Acceptance Rate65.8%