← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon OA for a SWE role, one algorithmic problem about rearranging an array to maximize the number of ascending adjacent pairs. Pretty clean problem once you see it.

Questions Asked (1)

Q1

Given an array of integers, find the maximum number of adjacent index pairs where the left value is strictly less than the right value, after reordering the array however you want.

Algorithms & Data Structures
Author's notes

I overthought this at first and started sketching out some greedy approach with sorting and adjacent swaps.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the problem asks for the maximum number of adjacent pairs (i, i+1) such that arr[i] < arr[i+1] after reordering. Then, recognize that the optimal strategy is to sort the array and interleave the smaller half with the larger half to maximize the number of such pairs. Finally, derive the formula: for n elements, the maximum is floor(n/2) if there are at least two distinct values, otherwise 0.

Pro tip: Mention that this is equivalent to maximizing the number of 'ascents' in a permutation, and that sorting plus interleaving achieves the theoretical upper bound of floor(n/2). Also, note that if all elements are equal, no pair satisfies the strict inequality.

1. Clarify the problem

Confirm that we are counting adjacent index pairs (i, i+1) where the left value is strictly less than the right value, and that we can reorder the array arbitrarily.

2. Identify the goal

We want to maximize the number of such pairs, which is equivalent to maximizing the number of ascents in the rearranged sequence.

3. Determine the upper bound

In any sequence of length n, there are n-1 adjacent pairs. However, not all can be ascents because the sequence cannot be strictly increasing throughout if there are duplicate values or if the maximum is not at the end. The maximum possible ascents is floor(n/2).

4. Construct an optimal arrangement

Sort the array. Split it into two halves: the smaller half (first floor(n/2) elements) and the larger half (remaining elements). Interleave them: place a smaller element, then a larger element, alternating. This ensures each smaller element is followed by a larger one, yielding floor(n/2) ascents.

5. Handle edge cases

If all elements are equal, no pair satisfies the strict inequality, so the answer is 0. If n < 2, answer is 0. Otherwise, the answer is floor(n/2) as long as there are at least two distinct values.

Key Points to Mention

  • The problem reduces to maximizing the number of ascents in a permutation.
  • The theoretical upper bound is floor(n/2) because each ascent requires a distinct 'peak' and 'valley' pairing.
  • Sorting and interleaving the two halves achieves this bound.
  • If all elements are equal, the answer is 0.
  • Time complexity: O(n log n) due to sorting, which is optimal for comparison-based sorting.
  • Space complexity: O(n) for the rearranged array, or O(1) if sorting in place and counting.

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