Split Modules Across Three Servers to Maximize the Minimum Deployment Difficulty
Company: J.P. Morgan
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Assign modules with given difficulty values to three non-empty servers so that the smallest value of |d1 - d2| + |d2 - d3|, over one module picked from each server, is as large as possible. Tests max-min reasoning over partitions, the role of the middle server, and handling of duplicates and large values.
Constraints
- 3 <= n <= 10^5
- 1 <= difficulty[i] <= 10^9
- The answer can approach 2 * 10^9; return it as a 64-bit integer in typed languages (long in Java, long long in C++).
Examples
Input: ([1, 5, 9],)
Expected Output: 12
Explanation: Example 1: minimum n = 3, one module per server; 1 or 9 on Server 2 gives 12, 5 there gives only 8.
Input: ([100, 1, 3, 2],)
Expected Output: 196
Explanation: Example 2: high outlier 100 alone on Server 2, 1 alone on Server 3, {2, 3} on Server 1: 97 + 99.
Hints
- For a fixed assignment, the cheapest pick first chooses a Server 2 module; after that, Server 1 and Server 3 each contribute the module closest in difficulty to it, independently of each other.
- Only the relative order of the difficulties decides which modules are closest, so look at the values in sorted order and ask where the smallest and largest values should go.
- Equal difficulties may be split across servers, and every server must stay non-empty; check both facts against any assignment you have in mind.