Maximum System Memory Capacity
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the constraint tells you the plan before you read the examples. With n up to 2 * 10^5, trying every subset of primaries and every backup assignment is dead on arrival. Even an O(n^2) pairing loop is too slow. You need sorting plus a single greedy pass, so O(n log n). The story is servers and backups, but the task is matching each primary to a backup that's at least as big, while maximizing the sum of the primaries only. If you blank on the matching logic mid-assessment, StealthCoder runs invisibly as a safety net and can hand you the solution on screen.
The problem
Amazon is optimizing the capacity of a cloud system with n servers. The memory capacity of the i-th server is memory[i]. A system uses an even number of servers. If it uses 2x servers, exactly x are primary servers and the other x are backup servers. For every primary server P, it must be paired with a distinct backup server B whose memory capacity is at least that of P. The system memory capacity is the sum of the memory capacities of all primary servers. You may leave servers unused. Given memory, return the maximum system memory capacity that can be formed. Complete maximumCapacity, which receives the integer array memory and returns the maximum capacity as a long. Function maximumCapacity(memory: int[]) → long Examples Example 1 memory = [1, 2, 1, 2] return = 3 Here, we have 4 servers [serverA, serverB, serverC, serverD] having memory sizes as [1, 2, 1, 2]. We can choose serverA and serverB as primary servers, and serverC and serverD as their respective backup. The conditions hold true since memory[serverC] ≥ memory[serverA] and memory[serverD] ≥ memory[serverB]. Hence, the maximum system memory capacity is 3. Example 2 memory = [1, 2, 1] return = 1 Here, we have 3 servers [serverA, serverB, serverC] having memory sizes as [1, 2, 1]. We can choose serverA as a primary server, and serverB as its respective backup server. The conditions hold true since memory[serverB] ≥ memory[serverA]. Hence, the maximum system memory capacity is 1. Example 3 memory = [2, 4, 3, 1, 2] return = 5 Given 5 servers as [serverA, serverB, serverC, serverD, serverE] having memory = [2, 4, 3, 1, 2]. Primary Servers Options Backup Servers Options Conditions Valid Option serverA, serverB serverC, serverD memory[serverA] <= memory[serverC], memory[serverB] <= memory[serverD] No serverA, serverD serverB, serverE memory[serverA] <= memory[serverB], memory[serverD] <= memory[serverE] Yes serverA, serverC serverE, serverB memory[serverA] <= memory[serverE], memory[serverC] <= memory[serverB] Yes In the second configuration, the system memory capacity is memory[serverA] + memory[serverD] = 3. While in the third configuration, it is memory[serverA] + memory[serverC] = 5. Hence, the maximum system memory capacity is 5. Constraints 2 ≤ n ≤ 2 * 10^5 1 ≤ size[i] ≤ 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that backups only need to be big enough, and their values never count toward the score. Sort the array first. Then the question becomes which elements to spend as primaries and which to burn as backups, so that the primaries you keep are as large as possible. A larger primary needs a larger backup, so the top of the sorted array is contested. Work through example 3 sorted as [1,2,2,3,4]. The answer 5 comes from primaries 2 and 3, backed by 2 and 4. The 1 gets left unused. That's the pitfall: you don't have to use every server, and an odd n always leaves one out. Another pitfall is overflow. Sums reach roughly 10^14, so use a 64-bit integer. Test the greedy on all three examples before submitting. If your pairing logic fails on the odd-length cases, StealthCoder is the hedge during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum System Memory Capacity cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum System Memory Capacity FAQ
How hard is Maximum System Memory Capacity really?+
Medium. The code is short once you see it, but the matching logic is easy to get wrong. The input size rules out brute force, so you need sorting and a greedy pass. Most mistakes come from forcing every server to be used or from using a 32-bit sum.
What's the trick for this Amazon OA question?+
Sort the memory array, then think of it as matching each primary to a distinct backup with capacity at least as large. Only the primaries count toward the sum, and unused servers are allowed. Pick the largest primaries that can still be matched, instead of pairing everything.
Why does the return type have to be a long?+
The sum can reach about 10^5 primaries times 10^9 each, which is around 10^14. That overflows a 32-bit int. Accumulate in a 64-bit type from the start, not just at the return, or you'll get wrong answers on the large hidden tests.
Do I have to use every server?+
No. The statement says you may leave servers unused. Example 2 with [1,2,1] uses only two servers and returns 1. Odd-length inputs always leave at least one out. Your solution has to be free to skip small or awkward values.
How do I prepare for this in 48 hours?+
Practice sorting-plus-greedy matching problems where one group must dominate another, and write them out in full. Then trace this problem's three examples by hand on sorted arrays, including the odd-length ones. Check complexity: the target is O(n log n), and you should expect large-n tests.