← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon SWE online assessment with a pairs-counting problem. Pretty standard OA format, one coding question with constraints that feel straightforward until you think about the edge cases around duplicates.

Questions Asked (1)

Q1

Given an integer array of stock profits (can be negative, zero, or positive) and a target integer, count the number of distinct value pairs (a, b) where a + b equals the target, using two different indices. A value can pair with itself only if it appears at least twice. Return the count of distinct value pairs.

Algorithms & Data Structures
Author's notes

The duplicate handling is where I almost messed up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that we need to count distinct value pairs (a, b) where a + b = target and a and b come from different indices, with the special case that a can equal b only if there are at least two occurrences of that value. Use a hash set to track seen values and another set to store unique pairs, iterating through the array and checking if target - current value exists in seen. Return the size of the pair set.

Pro tip: Mention that using a set for pairs automatically handles duplicates and the self-pairing condition, and discuss the trade-off between time and space complexity. Also, clarify edge cases like empty array or no pairs.

1. Clarify requirements and edge cases

Confirm that pairs are unordered and distinct by value, and that a value can pair with itself only if it appears at least twice. Ask about input size and constraints.

2. Choose data structures

Use a hash set to store seen values for O(1) lookups and another set to store unique pairs (as tuples or encoded values) to avoid duplicates.

3. Iterate and find pairs

For each number, compute complement = target - number. If complement is in seen, add the pair (min(number, complement), max(number, complement)) to the pair set. Then add number to seen.

4. Handle self-pairing

When number equals complement, ensure that the value appears at least twice. This is naturally handled because the first occurrence adds to seen, and the second occurrence will find the complement in seen.

5. Return result and analyze complexity

Return the size of the pair set. Discuss time complexity O(n) and space complexity O(n) due to the sets.

Key Points to Mention

  • Use of hash set for O(1) lookups to achieve linear time complexity.
  • Storing pairs in a set to ensure distinctness and avoid counting duplicates.
  • Handling the self-pairing condition by checking if the value appears at least twice.
  • Edge cases: empty array, no valid pairs, all elements same, negative numbers.
  • Time and space complexity analysis: O(n) time, O(n) space.
  • Alternative approaches like sorting and two-pointer, and why hash set is better for this problem.

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