FastPrepGet Max Efficiency
Problems/Get Max Efficiency

Get Max Efficiency

Salesforce logoSalesforce● HardFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

The manager oversees a set of n servers, each with a designated upgrade capacity represented by the array element capacity[i]. The goal is to create precisely k upgrade batches, where the number of servers in the ith batch is represented by the array element numServers[i] where 0 ≤ i < n.

The efficiency of an upgrade batch is determined by the difference between the maximum and minimum upgrade capacities of the servers within that batch. The manager's objective is to allocate servers to the upgrade batches in a way that maximizes the sum of efficiencies across all k batches. The task is to find the maximum sum of efficiency.

Note: Each server must be assigned to exactly one upgrade batch.

Function

getMaxEfficiency(capacity: int[], numServers: int[]) → long

Complete the function getMaxEfficiency in the editor below.

getMaxEfficiency takes the following parameters:

  1. int capacity[n]: the upgrade capacity of each server
  2. int numServers[k]: the number of servers in each upgrade batch

Returns long: the maximum possible sum of efficiency of k upgrade batches

Examples

Example 1

capacity = [3, 6, 1, 2]numServers = [1, 3]return = 5

One of the optimal ways is:

  • Batch 1 takes the first server. Therefore, the efficiency of the batch = 3 - 3 = 0
  • Batch 2 takes the servers at indices 1, 2, and 3. The efficiency of the batch = 6 - 1 = 5

Hence, the sum of efficiencies is 0 + 5 = 5.

Example 2

capacity = [1, 2, 3, 4]numServers = [4]return = 3

Since there is only one batch to upgrade all the servers, the efficiency of the batch is 4 - 1 = 3. Hence, the sum of efficiencies of all the batches (which is 1) is 3.

Example 3

capacity = [4, 2, 1]numServers = [1, 1, 1]return = 0

Since the size of each server upgrade batch is 1, all 3 servers are upgraded in different batches, and the efficiency of each batch is 0. Hence, the sum of efficiencies is 0.

Constraints

  • 1 ≤ n ≤ 2x10^5
  • 1 ≤ k ≤ n
  • 1 ≤ capacity[i] ≤ 10^9
  • 1 ≤ numServers[i] ≤ n
  • Σ numServers[i] = n

More Salesforce interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays With Product Equal to KOA · Seen Nov 2026
  • Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
  • Get Message StatusOA · Seen Nov 2026
  • Detect a Cycle in a Linked ListPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Maximize K-Element AND with an Increment BudgetAmazon · OA
  • Delete and Freeze GameRubrik · OARubrik interview questions
  • Most Segments That Sort the ArrayRippling · OARippling interview questions
  • Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
  • Luna and the Colorful SocksPhonePe · OAPhonePe interview questions
  • Minimum Starting Coins to Buy All ProductsMicrosoft · OAMicrosoft interview questions
  • Minimum Swaps to Sort a Ternary Array After UpdatesGoogle · OAGoogle interview questions
  • Finding a Hidden Benchmark ValueSamsung · OASamsung interview questions
  • Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public long getMaxEfficiency(int[] capacity, int[] numServers) {
      // write your code here
    }

}
Checking account…