Balanced team assignment by workload
Python data engineering interview problem. Difficulty: advanced. Pattern: Sorting. About 20 minutes. Part of the Pro drill bank.
A shift planner has a list of task efforts and a number of workers. Work should be split so no worker is overloaded relative to the others.
A manager has tasks with different amounts of effort and a number of workers, and wants the work spread evenly. The measure of success is the time the busiest worker spends. Assign tasks to workers with this rule: take the tasks from the largest to the smallest, and give each one to the worker who currently has the least work. Return the largest total workload at the end. If there are no tasks or no workers, return 0. Write assign_tasks(efforts, workers). For efforts [4, 2, 7, 1] and 2 workers: 7 goes to worker 1, 4 goes to worker 2, 2 goes to worker 2 (4 < 7) making 6, and 1 goes to worker 2 making 7. The busiest worker has 7. With no tasks the answer is 0. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Sort the tasks from largest to smallest.
- Give each task to the least loaded worker.
- Return the largest total at the end.
- Return 0 for no tasks or no workers.
Constraints
- efforts is a list of positive numbers.
- workers is a whole number.
Examples
Input: assign_tasks([4, 2, 7, 1], 2) Output: 7 This input follows the stated rules and produces this output.
Topics: lakebench, python, greedy.
More Python interview questions · All interview problems · Learn data engineering
Balanced team assignment by workload
Interview-style drill: Distribute a list of tasks with varying effort across a fixed number of workers to minimize the busiest worker's load.
Assign tasks to workers in a balanced way by workload.