← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon SWE coding assessment, one problem about minimizing cost to buy servers given power and price constraints. Pretty standard OA format, nothing surprising about the setup itself.

Questions Asked (1)

Q1

Given a list of servers each with a power value and a price of either 1 or 2 credits, find the minimum total cost to select a subset of servers whose combined power meets or exceeds a given target. Return -1 if it's impossible.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was greedy: grab all the price-1 machines sorted by power descending, then fill in with price-2 if still short.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a knapsack-like problem where items have weights (power) and costs (1 or 2). Since costs are only 1 or 2, we can sort servers by power descending within each cost group and use a greedy approach: try all possible numbers of cost-2 servers, and for each, fill the remaining target with the highest-power cost-1 servers. Alternatively, use dynamic programming with state (index, power) but optimize using the small cost values.

Pro tip: Clarify constraints upfront (e.g., number of servers, target size) to choose the right approach. Mention that if the target is large, a greedy with sorting might not be optimal, but with costs 1 and 2, we can prove that taking the highest power per cost is optimal after sorting.

1. Understand the problem and constraints

Restate the problem: select a subset of servers with total power >= target, minimizing total cost (each server costs 1 or 2). Ask about input size, target range, and whether powers are positive.

2. Identify the algorithmic pattern

This is a variant of the knapsack problem (min cost to achieve at least a target). Since costs are only 1 or 2, we can group servers by cost and sort each group by power descending.

3. Design an efficient solution

Use prefix sums of sorted powers for each cost group. Iterate over the number of cost-2 servers taken (from 0 to count2), compute the remaining power needed, and binary search or two-pointer to find the minimum number of cost-1 servers required. Track the minimum total cost.

4. Handle edge cases and return -1

If even taking all servers cannot meet the target, return -1. Also consider cases where target is 0 (cost 0) or no servers available.

5. Analyze complexity and trade-offs

Time complexity: O(n log n) for sorting plus O(n) for iteration. Space: O(n) for prefix sums. Discuss alternative DP approach and why greedy works here due to costs 1 and 2.

Key Points to Mention

  • Problem can be modeled as a knapsack variant with costs 1 and 2.
  • Sorting servers by power descending within each cost group ensures we pick the most powerful ones first.
  • Prefix sums allow O(1) query of total power for taking first k servers of a cost group.
  • Iterate over number of cost-2 servers, then compute minimum cost-1 servers needed using binary search or two pointers.
  • Time complexity O(n log n) due to sorting, which is optimal for this approach.
  • Edge cases: target 0, insufficient total power, and large input sizes.

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