Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Assign Tasks to Least Busy Agent

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Assign Tasks to Least Busy Agent. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Intercom's inbox assigns incoming conversations to available agents. Given each agent's processing capacity and a sequence of task workloads, assign every task to the agent with the smallest current workload relative to capacity.

An agent's busy time is defined as assigned_workload / capacity. After assigning a task, update that agent's assigned workload. If multiple agents have the same busy time, choose the agent with the smallest index. Tasks are assigned in the order provided, and every task must be assigned exactly once.

Formal Specification

Implement assign_tasks(capacities, tasks), where capacities is a list of positive integers and tasks is a list of positive integer workloads. Return a list containing the selected zero-based agent index for each task.

Comparisons must be exact. For example, 2 / 3 must be considered less than 1 / 2 only when mathematically appropriate, rather than based on rounded floating-point values.

Constraints

  • 1 <= len(capacities) <= 10^5
  • 1 <= len(tasks) <= 10^5
  • 1 <= capacities[i] <= 10^9
  • 1 <= tasks[i] <= 10^9
  • All assignments are made sequentially in task order

Function Signature

def assign_tasks(capacities, tasks):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output