Implement Due Broadcast Sender
Company: Attentive
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates a candidate's ability to implement time-based broadcast delivery, including filtering by time windows, idempotent state updates to prevent duplicate sends, and managing concurrency and data structures for processing and tracking broadcast logs.
Read the full Attentive Software Engineer interview experience this question came from
Part 1: Send Due Broadcasts Up To current_time
Constraints
- 0 <= number of companies <= 10^4
- 0 <= number of logs <= 10^4
- The total number of subscriber IDs across all companies is <= 10^5
- 0 <= scheduled_at, current_time <= 10^9
- Each `log_id` is unique
- Subscriber IDs within one company's list are distinct
Examples
Input: ({1: [101, 102], 2: [201]}, [{'log_id': 1, 'company_id': 1, 'message': 'A', 'scheduled_at': 5, 'sent': False}, {'log_id': 2, 'company_id': 2, 'message': 'B', 'scheduled_at': 8, 'sent': False}, {'log_id': 3, 'company_id': 1, 'message': 'C', 'scheduled_at': 4, 'sent': True}], 5)
Expected Output: ([(101, 1, 'A'), (102, 1, 'A')], [{'log_id': 1, 'company_id': 1, 'message': 'A', 'scheduled_at': 5, 'sent': True}, {'log_id': 2, 'company_id': 2, 'message': 'B', 'scheduled_at': 8, 'sent': False}, {'log_id': 3, 'company_id': 1, 'message': 'C', 'scheduled_at': 4, 'sent': True}])
Explanation: Only log 1 is both due and unsent at time 5. Log 2 is not due yet, and log 3 was already sent.
Input: ({1: [7], 2: [8, 9]}, [{'log_id': 10, 'company_id': 2, 'message': 'Sale', 'scheduled_at': 1, 'sent': False}, {'log_id': 11, 'company_id': 1, 'message': 'Ping', 'scheduled_at': 3, 'sent': False}], 10)
Expected Output: ([(8, 10, 'Sale'), (9, 10, 'Sale'), (7, 11, 'Ping')], [{'log_id': 10, 'company_id': 2, 'message': 'Sale', 'scheduled_at': 1, 'sent': True}, {'log_id': 11, 'company_id': 1, 'message': 'Ping', 'scheduled_at': 3, 'sent': True}])
Explanation: Both logs are due by time 10, so both are delivered in log order.
Hints
- For each log, first decide whether it is both due and unsent before generating any deliveries.
- To avoid duplicate sends in future calls, mark a log as sent immediately after processing it, even if it had no subscribers.
Part 2: Send Broadcasts Inside an Inclusive Time Window
Constraints
- 0 <= number of companies <= 10^4
- 0 <= number of logs <= 10^4
- The total number of subscriber IDs across all companies is <= 10^5
- 0 <= start_time <= end_time <= 10^9
- Each `log_id` is unique
- Subscriber IDs within one company's list are distinct
Examples
Input: ({1: [101, 102], 2: [201]}, [{'log_id': 1, 'company_id': 1, 'message': 'A', 'scheduled_at': 5, 'sent': False}, {'log_id': 2, 'company_id': 2, 'message': 'B', 'scheduled_at': 8, 'sent': False}, {'log_id': 3, 'company_id': 1, 'message': 'C', 'scheduled_at': 10, 'sent': False}], 5, 8)
Expected Output: ([(101, 1, 'A'), (102, 1, 'A'), (201, 2, 'B')], [{'log_id': 1, 'company_id': 1, 'message': 'A', 'scheduled_at': 5, 'sent': True}, {'log_id': 2, 'company_id': 2, 'message': 'B', 'scheduled_at': 8, 'sent': True}, {'log_id': 3, 'company_id': 1, 'message': 'C', 'scheduled_at': 10, 'sent': False}])
Explanation: Logs scheduled at 5 and 8 are inside the inclusive window [5, 8]. Log 3 is outside.
Input: ({1: [7]}, [{'log_id': 1, 'company_id': 1, 'message': 'X', 'scheduled_at': 6, 'sent': True}, {'log_id': 2, 'company_id': 1, 'message': 'Y', 'scheduled_at': 9, 'sent': False}], 6, 9)
Expected Output: ([(7, 2, 'Y')], [{'log_id': 1, 'company_id': 1, 'message': 'X', 'scheduled_at': 6, 'sent': True}, {'log_id': 2, 'company_id': 1, 'message': 'Y', 'scheduled_at': 9, 'sent': True}])
Explanation: Log 1 is in the time window but is already sent, so only log 2 is processed.
Hints
- The window is inclusive, so broadcasts exactly at `start_time` or `end_time` should be processed.
- Keep the filtering condition separate from the delivery loop: first decide whether a log belongs in the window and is unsent, then send it.