← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Apr 2026Remote

Summary

Amazon SWE coding round, one question the whole session: weighted random index. Felt manageable but the follow-up about efficiency under repeated calls is where things got interesting.

Questions Asked (1)

Q1

Design a data structure that takes an array of weights and supports a pickIndex() method that returns a random index with probability proportional to its weight. It needs to handle up to 200k elements and up to 200k calls efficiently.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was just to build a giant array repeating each index by its weight and sample from it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use prefix sums to convert weights into cumulative ranges, then binary search to find the index for a random target. This gives O(n) preprocessing and O(log n) per pick, meeting the scale requirements. Discuss trade-offs like memory and alternative approaches (e.g., segment tree) to show depth.

Pro tip: Mention that using a random double and binary search on prefix sums is standard, but also note that you can optimize by using a random integer in [0, totalWeight) and searching for the first prefix sum greater than that value. This avoids floating-point precision issues and is slightly faster.

1. Clarify requirements and constraints

Confirm that weights are positive, the array is static, and pickIndex will be called many times. Discuss expected time complexity for initialization and pickIndex.

2. Propose prefix sum + binary search

Explain that you'll compute prefix sums of weights, then for each pick, generate a random number between 0 and total weight, and binary search for the first prefix sum greater than that number.

3. Analyze complexity and trade-offs

State that preprocessing is O(n) time and space, and each pick is O(log n). Compare with alternatives like linear scan (O(n) per pick) or segment tree (O(log n) but more complex).

4. Handle edge cases and optimizations

Discuss handling zero weights, large total weight (use 64-bit integers), and potential optimizations like early termination or using a random integer to avoid floating-point issues.

5. Implement and test

Write clean code for the data structure, and suggest testing with small examples and verifying distribution with a large number of calls.

Key Points to Mention

  • Prefix sums array to represent cumulative weights
  • Binary search (e.g., std::upper_bound or custom) to find the index
  • Time complexity: O(n) preprocessing, O(log n) per pick
  • Space complexity: O(n) for prefix sums
  • Handling large weights with 64-bit integers to avoid overflow
  • Alternative approaches: segment tree, Fenwick tree, or rejection sampling (less efficient)

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