← Amazon Interview Insights

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

Intermediate
Apr 2026

Summary

Amazon SWE interview with a sliding window / sorting problem about buying servers that form a valid circular arrangement. Pretty clean problem once you see the trick, but the circular constraint is easy to miss on first read.

Questions Asked (1)

Q1

Given a list of servers each with a computing power value, find the maximum number of servers K a client can purchase such that the servers can be rearranged into a circular network where adjacent servers differ in power by at most 1.

Algorithms & Data Structures
Author's notes

Sort the array first, that part clicked pretty fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, recognize that the circular arrangement condition implies that the multiset of selected servers must have a maximum difference of at most 1 between any two values, because in a cycle every adjacent pair differs by at most 1, so the entire set must lie within a range of size 1. Then, group servers by their power values and for each possible pair of consecutive values (x, x+1), compute the maximum number of servers that can be selected from these two groups while ensuring the cycle can be formed (which requires at least two servers if only one value is used, or a balanced selection if two values are used). Finally, return the maximum over all such pairs.

Pro tip: Clarify the problem constraints early: ask whether K can be 1 (a single server is trivially a cycle) and whether the input can have duplicate power values. This shows attention to edge cases and avoids incorrect assumptions.

1. Understand the circular arrangement condition

Explain that in a cycle, every adjacent pair must differ by at most 1, so the entire set of selected servers must have values that are either all equal or differ by exactly 1. Thus, the selected servers can only come from at most two consecutive power values.

2. Group servers by power value

Count the frequency of each power value using a hash map or by sorting. This will help efficiently compute the maximum number of servers that can be selected from any two consecutive values.

3. Analyze feasible selections for a pair of values

For a pair (x, x+1) with counts c1 and c2, determine the maximum number of servers that can be arranged in a cycle. If only one value is used, need at least 2 servers (unless K=1 is allowed). If both values are used, the counts must satisfy that the number of x's and x+1's differ by at most 1? Actually, in a cycle with two values, the sequence must alternate, so the counts must be equal or differ by at most 1? Wait, careful: In a cycle, if we have only two distinct values, they must alternate, so the counts must be equal or differ by exactly 1? Let's think: For a cycle of length K with values a and b, adjacent must differ by at most 1. If a and b differ by 1, then any adjacent pair of different values is fine, but same values are also fine (difference 0). So we can have any arrangement as long as no two adjacent differ by more than 1. But if we only have two values that differ by 1, then any arrangement works because any adjacent pair will have difference either 0 or 1. So the condition is automatically satisfied! Wait, that's a key insight: If all selected servers have values that are either x or x+1, then any adjacent pair will have difference at most 1. So the circular arrangement condition is automatically satisfied for any multiset of servers whose values are within a range of 1. Therefore, the problem reduces to: find the maximum number of servers that can be selected such that all selected servers have values in some interval [x, x+1] (i.e., at most two consecutive distinct values). But is that true? Let's verify: Suppose we have servers with values 1,2,3. Can we arrange them in a cycle? If we take all three, we have 1,2,3. In a cycle, we must place them in some order. If we place 1-2-3-1, then adjacent pairs: 1-2 (diff 1), 2-3 (diff 1), 3-1 (diff 2) -> fails. So we cannot take all three. So indeed, the selected set must have max-min <= 1. So the condition is exactly that the selected servers' values lie within a range of size 1. So the problem is: given a list of numbers, find the maximum number of elements that can be chosen such that the difference between the maximum and minimum chosen is at most 1. That is equivalent to: for each possible value x, consider all servers with value x and x+1, and take all of them? But wait, if we take all servers with value x and all with value x+1, then max-min = 1, so it's valid. So the maximum K is simply the maximum over all x of (count(x) + count(x+1)). But is that always achievable? Yes, because any arrangement of these servers will have adjacent differences at most 1. So the answer is simply the maximum sum of frequencies of two consecutive values. However, we must also consider the case where we take only one value: then K = count(x), but that is included in the sum if we consider x and x+1 with count(x+1)=0? Actually, if we take only one value, max-min=0, which is <=1, so it's valid. So the maximum over all x of (count(x) + count(x+1)) covers that if we allow x+1 to be a value not present (count=0). But we need to consider all possible x, including those not in the list? Actually, if we take only one value, say x, then K = count(x). This is equal to count(x) + count(x+1) if count(x+1)=0. So we can just consider all distinct values present and also consider x+1 even if not present. So the answer is max over all x in the set of values of (count(x) + count(x+1)), where count(y)=0 if y not present. But wait, what if we take only one value and that value has count=1? Then K=1. Is a single server a valid circular network? Typically, a cycle of length 1 is allowed? The problem says 'circular network' - usually a cycle requires at least 3? But in graph theory, a cycle of length 1 is a self-loop, but here it's a network of servers, so probably K>=2? The problem statement: 'maximum number of servers K a client can purchase such that the servers can be rearranged into a circular network where adjacent servers differ in power by at most 1.' It doesn't specify minimum K. Often in such problems, K can be 1. But we should clarify. If K=1 is allowed, then answer is at least 1. If not, then we need at least 2. But the sum approach still works if we consider that for a single value, we need at least 2 servers to form a cycle? Actually, if we have two servers with the same value, they can be arranged in a cycle of length 2? A cycle of length 2 would have two edges between the same pair? Typically, a cycle in a simple graph requires at least 3 vertices. But here it's a circular arrangement, so a cycle of length 2 might be considered as two servers connected in a circle? Usually, a circular arrangement of 2 items is just a line? The problem likely expects K>=2. But we can handle by ensuring that if we take only one value, we need at least 2 servers. So the maximum for a single value is count(x) if count(x)>=2, else 0? But if count(x)=1, can we take that one server? Probably not, because a cycle of length 1 is not a network. So we should assume K>=2. Then the answer is max over x of (count(x) + count(x+1)) but with the caveat that if we take only one value, we need count(x)>=2. However, if we take two values, we can take any number as long as both counts are positive? Actually, if we take both x and x+1, we can take all of them, and the cycle will be valid as long as K>=2. So the maximum is simply the maximum over all x of (count(x) + count(x+1)), but if that maximum is 1 (i.e., all counts are 1 and no two consecutive values both present), then we cannot form a cycle of length 1, so we might need to return 0? But the problem likely expects at least 2. So we should consider that. However, typical solutions for this problem (like on LeetCode) assume K can be 1? Actually, there is a known problem: 'Maximum Number of Servers' or something. I recall a problem: given an array, find the maximum number of elements that can be selected such that the difference between max and min is at most 1. That is exactly the sum of frequencies of two consecutive values. And that problem does not have a minimum K constraint; it just asks for the maximum size of a subset with max-min <=1. So K can be 1. So the answer is simply max over x of (count(x) + count(x+1)). So the approach is: count frequencies, then for each distinct value x, compute count(x) + count(x+1) (where count(x+1) is 0 if not present), and take the maximum. That's O(n) time and O(n) space. But we must also consider that if we take only one value, we can take all of them, so that's covered. So the solution is straightforward. However, the problem might have a twist: the circular arrangement condition

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