Implement Due Broadcast Sender
Company: Attentive
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: 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.
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.
Input: ({1: [1]}, [{'log_id': 5, 'company_id': 99, 'message': 'Ghost', 'scheduled_at': 2, 'sent': False}], 2)
Expected Output: ([], [{'log_id': 5, 'company_id': 99, 'message': 'Ghost', 'scheduled_at': 2, 'sent': True}])
Explanation: The company has no subscribers, so no deliveries are recorded, but the due log is still marked sent.
Input: ({}, [], 100)
Expected Output: ([], [])
Explanation: Edge case: no companies and no logs.
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.
Input: ({3: [30, 31]}, [{'log_id': 4, 'company_id': 3, 'message': 'Exact', 'scheduled_at': 12, 'sent': False}, {'log_id': 5, 'company_id': 3, 'message': 'Late', 'scheduled_at': 13, 'sent': False}], 12, 12)
Expected Output: ([(30, 4, 'Exact'), (31, 4, 'Exact')], [{'log_id': 4, 'company_id': 3, 'message': 'Exact', 'scheduled_at': 12, 'sent': True}, {'log_id': 5, 'company_id': 3, 'message': 'Late', 'scheduled_at': 13, 'sent': False}])
Explanation: Edge case: start and end times are equal, so only logs exactly at time 12 are processed.
Input: ({1: [1]}, [{'log_id': 6, 'company_id': 9, 'message': 'NoList', 'scheduled_at': 2, 'sent': False}], 1, 3)
Expected Output: ([], [{'log_id': 6, 'company_id': 9, 'message': 'NoList', 'scheduled_at': 2, 'sent': True}])
Explanation: The log is inside the window, but the company has no subscribers. It is still marked as sent.
Input: ({}, [], 0, 0)
Expected Output: ([], [])
Explanation: Edge case: no companies and no logs.
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.