Design an hourly call-agent scheduler
Hippocratic AiSoftware Engineer · OnsiteLast updated
This problem tests your ability to model resource scheduling under capacity constraints and optimize the assignment of tasks to workers over discrete time slots.
Problem Statement
You are building a scheduler for a call-agent platform. You are given a list of tasks (calls), where each task has a start_time and a duration (in hours). You also have a set of agents available to handle these calls.
Constraints:
- Each agent can handle at most one call at any given time.
- Each task must be assigned to an agent who is available for the entire duration of the task.
- A single customer (identified by
customer_id) can have multiple calls, but they cannot have more thanmax_concurrencycalls occurring simultaneously.
Implement a function schedule_tasks(tasks, max_concurrency) that returns a dictionary mapping task_id to agent_id. If it is impossible to schedule all tasks, return an empty dictionary.
Input Format
tasks: A list of dictionaries:[{'task_id': int, 'customer_id': int, 'start_time': int, 'duration': int}, ...]max_concurrency: An integer representing the limit of simultaneous calls per customer.
Example
tasks = [
{'task_id': 1, 'customer_id': 101, 'start_time': 0, 'duration': 2},
{'task_id': 2, 'customer_id': 101, 'start_time': 1, 'duration': 2}
]
# If max_concurrency = 1, these cannot be scheduled together.
Constraints
0 <= start_time < 241 <= duration <= 241 <= len(tasks) <= 1000- Time is measured in integer hourly blocks.
Hints
Hint 1
Sort the tasks by start time to process the schedule chronologically.
Hint 2
Maintain a data structure to track which agent is busy until what hour, and another to track current active calls per customer.
Hint 3
Since this is a scheduling problem, consider a greedy approach using a min-heap to keep track of available agents.
Follow-up questions
- How would you modify your solution if agents have different 'skill levels' and certain tasks require a minimum skill level?
- What changes are needed if the system needs to support real-time rescheduling when a call duration exceeds its initial estimate?
- How would you scale this system if you had millions of tasks and thousands of agents across different time zones?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.