KatInterview

Event-Driven Email Notification System with Account Updates

Data Structure DesignModel answer
MediumModel answerData Structure Designheapssimulationevent-driven

Last updated

Build a scheduler that emits subscription notifications in time order, handling plan switches and subscription extensions with deterministic tie-breaking.

You're building an automated email system for a subscription service. The system must schedule and emit notification messages based on account lifecycles, plan modifications, and subscription extensions.

Input:

  1. send_schedule: A dictionary mapping when to send notifications:

    • "start": Label for the account start date
    • Negative integers (e.g., -7, -30): Labels for notifications sent that many days before expiration
    • "end": Label for the expiration date
  2. user_accounts: List of account records, each containing:

    • name: Account identifier (string)
    • plan: Plan type (string)
    • account_date: Start day (integer)
    • duration: Subscription length in days (integer)
  3. changes (optional): List of updates, each being either:

    • Plan switch: {name, new_plan, change_date}
    • Extension: {name, extension, change_date} (adds days to subscription)

Output:

Emit all notifications as text lines in chronological order:

<day>: [<Label>] Subscription for <name> (<plan>)

Tie-breaking rules when multiple events occur on the same day:

  1. State updates first: plan switches, then extensions (in that order)
  2. Start notifications
  3. Pre-expiration warnings (sorted by offset: -30 before -7)
  4. End notifications
  5. Within each category, alphabetical by account name

Requirements:

  • Part A: Generate baseline notifications (start, offset warnings, end) for all accounts
  • Part B: Handle plan switches—update takes effect immediately, all notifications from that point forward reflect the new plan
  • Part C: Handle extensions—recalculate expiration and all future offset/end notifications based on the new expiration date

Example:

send_schedule = {"start": "Welcome", -15: "Expiring Soon", "end": "Expired"}
accounts = [
    {"name": "alice", "plan": "Basic", "account_date": 0, "duration": 30}
]
changes = [
    {"name": "alice", "new_plan": "Premium", "change_date": 10}
]

Output includes:

0: [Welcome] Subscription for alice (Basic)
10: [Changed] Subscription for alice (Premium)
15: [Expiring Soon] Subscription for alice (Premium)
30: [Expired] Subscription for alice (Premium)

Describe your data structures, algorithm, and time complexity. State any assumptions clearly.

Hints

Hint 1

Use a priority queue keyed by (time, event_type, name) to merge all notification streams efficiently

Hint 2

When an extension occurs, previously scheduled offset and end notifications become invalid—use versioning to identify stale events

Hint 3

Plan switches must fire before any other event at the same timestamp to ensure subsequent notifications reflect the updated plan