Check Distribution: Verify Load Is Evenly Distributed Across Servers
Interview Experience
Problem
You have n servers and a list of request assignments (server_id, request_weight). A distribution is considered "balanced" if the maximum total weight on any server minus the minimum total weight is at most threshold. Write a function that checks whether a given assignment is balanced and, if not,
returns the servers causing the imbalance.
python
def check_distribution(
n: int,
assignments: list[tuple[int, float]],
threshold: float
) -> tuple[bool, list[int]]:
"""Return (is_balanced, list_of_imbalanced_server_ids)."""
pass
**Input**:
n=3, threshold=5.0
assignments = [(0,10),(1,8),(2,20),(0,5),(1,7)]
# Server totals: 0->15, 1->15, 2->20
# max-min = 20-15 = 5 <= 5 -> balanced
Output: (True, [])
assignments = [(0,10),(1,3),(2,20)]
# Totals: 0->10, 1->3, 2->20. max-min=17 > 5
Output: (False, [1, 2])
Follow-ups
- Define "imbalanced server" clearly — is it servers above average, below average, or at both extremes?
- How would you rebalance by moving the minimum number of requests?
- Extend to a real-time streaming version where
check_distributionis called after each new assignment. - How would you write a SQL query to find imbalanced servers if assignments are stored in a table?
Full Details
Problem
You have n servers and a list of request assignments (server_id, request_weight). A distribution is considered "balanced" if the maximum total weight on any server minus the minimum total weight is at most threshold. Write a function that checks whether a given assignment is balanced and, if not,
returns the servers causing the imbalance.
python
def check_distribution(
n: int,
assignments: list[tuple[int, float]],
threshold: float
) -> tuple[bool, list[int]]:
"""Return (is_balanced, list_of_imbalanced_server_ids)."""
pass
**Input**:
n=3, threshold=5.0
assignments = [(0,10),(1,8),(2,20),(0,5),(1,7)]
# Server totals: 0->15, 1->15, 2->20
# max-min = 20-15 = 5 <= 5 -> balanced
Output: (True, [])
assignments = [(0,10),(1,3),(2,20)]
# Totals: 0->10, 1->3, 2->20. max-min=17 > 5
Output: (False, [1, 2])
Follow-ups
- Define "imbalanced server" clearly — is it servers above average, below average, or at both extremes?
- How would you rebalance by moving the minimum number of requests?
- Extend to a real-time streaming version where
check_distributionis called after each new assignment. - How would you write a SQL query to find imbalanced servers if assignments are stored in a table?
About This Question
This is a candidate experience report from a perplexity interview during the phone round.
It covers the following topics: Phone, Sql, System Design, Coding, Onsite .