Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Basic Load Balancing with Link Weights

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

Your question is Basic Load Balancing with Link Weights. 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

NVIDIA networking components may expose several links with different capacities. Implement a deterministic load balancer that distributes requests proportionally to integer link weights while avoiding unnecessary bursts on any one link.

Use the smooth weighted round-robin algorithm. For every request, add each link's weight to its running score, select the link with the highest score, subtract the sum of all weights from the selected link's score, and record that link's index. Break ties by choosing the lowest index.

Formal Specification

Implement distribute_requests(weights, request_count).

  • weights is a list of non-negative integers, where weights[i] is the capacity weight of link i.
  • request_count is a non-negative integer.
  • Return a list of request_count integers. Each integer is the selected link index.
  • At least one weight is positive. Links with weight 0 must never receive requests.

Constraints

  • 1 <= len(weights) <= 100
  • 0 <= weights[i] <= 10^6
  • sum(weights) > 0
  • 0 <= request_count <= 10^5

Function Signature

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