Magicsheet logo

Maximum Number of Upgradable Servers

Medium
81.8%
Updated 6/1/2025

Asked by 1 Company

Maximum Number of Upgradable Servers

What is this problem about?

The Maximum Number of Upgradable Servers coding problem presents a scenario where you have n servers, each with a certain capacity and cost. You want to upgrade some servers such that the total capacity of the upgraded servers is maximized, while keeping the total cost within a given budget. Additionally, there might be a constraint that an upgraded server's capacity must be at least min_capacity.

Why is this asked in interviews?

Snowflake and similar companies use this problem to test dynamic programming or greedy approaches, often combined with sorting or binary search. It's a resource allocation problem that evaluates a candidate's ability to model constraints and optimize a target value. The "upgradable" implies a choice for each server, often leading to a knapsack-like problem or a variant that can be simplified.

Algorithmic pattern used

Binary Search on the Answer + Greedy/DP Check: This problem is often a variation of the knapsack problem. If the constraints on capacity and cost are such that capacity values are large but cost values are small (or vice-versa), then a standard DP approach can be used. If the "number of upgradable servers" is what we want to maximize, and not total capacity, then it changes the problem.

Let's assume the question means: maximize the number of servers you can upgrade, given budget and min_capacity per server.

  1. Filter: First, filter out any servers whose capacity is less than min_capacity, as they are not eligible for upgrade.
  2. Sort: Sort the remaining eligible servers by their cost in ascending order.
  3. Greedy Selection: Iterate through the sorted servers. Greedily pick servers one by one as long as your budget is sufficient. Count the number of servers picked. This gives the maximum number of servers if we only consider cost.

However, if the problem is "maximize total capacity within budget," it's a 0/1 knapsack variant. If we can upgrade servers partially, it becomes a fractional knapsack.

Given the topics "Array, Math, Binary Search", it's likely a simpler variant or one that allows binary search on the result. If we want to maximize the number of upgraded servers:

  • Binary Search on k: If we can upgrade k servers, we can also upgrade k-1 servers. This monotonicity allows binary search on k (the number of servers).
  • check(k) function: Can we upgrade k servers within the budget?
    1. Filter eligible servers (capacity >= min_capacity).
    2. Sort these eligible servers by cost.
    3. Sum the cost of the cheapest k eligible servers. If this sum is <= budget, return true. Else, false. This check(k) takes O(N log N) for sorting and O(k) for summing. Overall O(N log N) for the binary search.

Example: servers = [(10, 5), (20, 8), (5, 3), (12, 6)], budget = 15, min_capacity = 10.

  1. Filter: Eligible servers: [(10, 5), (20, 8), (12, 6)]. ((5,3) is out).
  2. Sort by cost: [(10, 5), (12, 6), (20, 8)].

Binary search for k (number of servers): [0, 3]. Try k=2:

  • Cheapest 2 eligible servers: (10,5) and (12,6).
  • Total cost: 5 + 6 = 11.
  • 11 <= budget (15). Yes. ans = 2, low = 3. Try k=3:
  • Cheapest 3 eligible servers: (10,5), (12,6), (20,8).
  • Total cost: 5 + 6 + 8 = 19.
  • 19 > budget (15). No. high = 2.

Result ans = 2.

Common mistakes candidates make

  • Not filtering first: Including ineligible servers in the cost calculation.
  • Incorrect sorting criterion: If maximizing count, sort by cost. If maximizing capacity, it's a knapsack (more complex).
  • Misunderstanding "upgradable": Clarify what "upgradable" means (e.g., all have capacity X, or capacity must be >= X).

Interview preparation tip

For the Array Math Binary Search interview pattern, this problem reinforces the idea of binary searching on the answer when a monotonic property exists. Often, the check function for such problems involves sorting and a greedy choice.

Similar Questions