← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Apr 2026

Summary

Amazon SWE online assessment with a greedy pairing problem. Nothing too wild but the constraint around backup memory being >= primary tripped me up at first.

Questions Asked (1)

Q1

Given an array where each element represents a server's memory, pair servers such that each pair has one primary and one backup, where the backup's memory must be greater than or equal to the primary's. Maximize the total memory sum of all primary servers. Each server can only belong to one pair.

Algorithms & Data Structures
Author's notes

Took me a minute to see the right move here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Sort the array and use a greedy two-pointer strategy: pair the largest available server as a backup with the largest possible primary that it can cover, ensuring each primary is as large as possible. This maximizes the sum of primaries because each backup can only cover one primary, and we want to assign the largest backups to the largest primaries they can support.

Pro tip: Clarify that the goal is to maximize the sum of primary memories, not the number of pairs. Mention that if the array length is odd, one server will be left unpaired, and it's optimal to leave the smallest server unpaired.

1. Understand the problem and constraints

Restate the problem: pair each primary with a backup such that backup >= primary, each server used once, maximize sum of primaries. Note that the array can be sorted without loss of generality.

2. Sort the array

Sort the memory values in non-decreasing order. This allows efficient pairing using two pointers or binary search.

3. Greedy pairing from largest to smallest

Use two pointers: one at the end (largest) for backup, one at the middle for primary. For each backup from largest to smallest, find the largest primary that is <= backup and not yet paired. Pair them and move pointers accordingly.

4. Handle odd length and edge cases

If the array length is odd, leave the smallest element unpaired. Also handle cases where no valid pairing exists (e.g., all elements equal but odd count).

5. Compute and return the sum

Sum the memories of all chosen primary servers and return the total. Verify with examples.

Key Points to Mention

  • Sorting the array to enable efficient pairing
  • Greedy strategy: pair largest possible backup with largest possible primary it can cover
  • Two-pointer technique to achieve O(n log n) time due to sorting
  • Proof of optimality: exchange argument showing greedy choice is safe
  • Handling odd-length arrays by leaving the smallest element unpaired
  • Time and space complexity analysis: O(n log n) time, O(1) extra space if sorting in place

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.