Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Load Balancing Algorithm

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

Your question is Load Balancing Algorithm. 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

Cruise can distribute autonomous-vehicle compute requests across a fixed pool of servers. Each request has a known processing duration and must be assigned immediately without moving previously assigned work. Implement a greedy load balancer that assigns every request to the server with the smallest current total load.

Formal Specification

Implement load_balance(server_loads, requests), where server_loads is an array of nonnegative integers representing each server's existing workload, and requests is an array of nonnegative integers representing processing durations in arrival order. Return an array assignments where assignments[i] is the zero-based server index selected for requests[i].

If multiple servers have the same minimum load, choose the server with the smallest index. After assigning a request, add its duration to that server's load before processing the next request.

Constraints

  • 1 <= len(server_loads) <= 10^5
  • 0 <= len(requests) <= 10^5
  • 0 <= server_loads[i], requests[i] <= 10^9
  • Server indices are zero-based
  • Assignments are made in request order

Function Signature

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