Design a class that adds numbers from a stream and checks whether any two of them sum to a target

Quick Overview

Design a class that stores a stream of integers and answers whether any two stored numbers add up to a target value. It tests choosing an internal representation, handling repeated values correctly, and weighing the cost of adding a number against the cost of answering a query.

Design a class that adds numbers from a stream and checks whether any two of them sum to a target

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a data structure that accepts a stream of integers and checks whether it holds a pair of integers that sum to a particular value. Implement a `TwoSum` class that adds numbers to an internal data structure and answers whether any pair of the stored numbers adds up to a given target. It has these methods: - `add(number: int) -> None` adds `number` to the data structure. - `find(value: int) -> bool` returns `True` if two stored numbers sum to `value`, otherwise `False`. A pair uses two separate additions, so a number can pair with itself only when it was added at least twice. Example 1: ```text Calls: ["TwoSum", "add", "add", "add", "find", "find"] Args: [[], [1], [3], [5], [4], [7]] Output: [null, null, null, null, true, false] ``` After adding 1, 3 and 5, `find(4)` is true because 1 + 3 = 4. `find(7)` is false because the only pair sums are 4, 6 and 8. Example 2: ```text Calls: ["TwoSum", "add", "add", "find", "find"] Args: [[], [3], [3], [6], [5]] Output: [null, null, null, true, false] ``` 3 was added twice, so `find(6)` is true (3 + 3). No pair of stored numbers sums to 5, so `find(5)` is false. ```hint What to store A plain set of numbers loses one fact that `find` needs. Think about a target that is exactly twice one of the stored numbers. ``` ### Constraints & Assumptions - `-10^5 <= number <= 10^5` - `-2^31 <= value <= 2^31 - 1` - At most `10^4` calls in total are made to `add` and `find`. ### Clarifying Questions to Ask - Which operation is called more often, `add` or `find`, and by roughly how much? - Is there a memory budget, or may the structure keep extra precomputed data to answer queries faster? - Can numbers ever be removed, or does the structure only grow? ### What a Strong Answer Covers - An internal representation that supports both operations, with the reason for choosing it. - Correct results when the target is twice a stored number, both when that number was added once and when it was added more than once. - The time and space cost of `add` and `find`. - The trade-off between making `add` cheap and making `find` cheap, tied to the expected mix of calls. ### Follow-up Questions - If `find` were called far more often than `add`, how would you redesign the class, and what would the change cost? - How would you support a `remove(number)` operation? - In Java or C++, what can go wrong when you compute `value - number` for targets near the 32-bit limits? - How would you make the class safe to call from several threads at once?

Overview: Design a class that stores a stream of integers and answers whether any two stored numbers add up to a target value. It tests choosing an internal representation, handling repeated values correctly, and weighing the cost of adding a number against the cost of answering a query.

Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a data structure that accepts a stream of integers and checks whether it holds a pair of integers that sum to a particular value.

Implement a TwoSum class that adds numbers to an internal data structure and answers whether any pair of the stored numbers adds up to a given target. It has these methods:

  • add(number: int) -> None adds number to the data structure.
  • find(value: int) -> bool returns True if two stored numbers sum to value , otherwise False .

A pair uses two separate additions, so a number can pair with itself only when it was added at least twice.

Example 1:

Calls:  ["TwoSum", "add", "add", "add", "find", "find"]
Args:   [[],       [1],   [3],   [5],   [4],    [7]]
Output: [null,     null,  null,  null,  true,   false]

After adding 1, 3 and 5, find(4) is true because 1 + 3 = 4. find(7) is false because the only pair sums are 4, 6 and 8.

Example 2:

Calls:  ["TwoSum", "add", "add", "find", "find"]
Args:   [[],       [3],   [3],   [6],    [5]]
Output: [null,     null,  null,  true,   false]

3 was added twice, so find(6) is true (3 + 3). No pair of stored numbers sums to 5, so find(5) is false.

Constraints & Assumptions

  • -10^5 <= number <= 10^5
  • -2^31 <= value <= 2^31 - 1
  • At most 10^4 calls in total are made to add and find .

Clarifying Questions to Ask Guidance

  • Which operation is called more often, add or find , and by roughly how much?
  • Is there a memory budget, or may the structure keep extra precomputed data to answer queries faster?
  • Can numbers ever be removed, or does the structure only grow?

What a Strong Answer Covers Guidance

  • An internal representation that supports both operations, with the reason for choosing it.
  • Correct results when the target is twice a stored number, both when that number was added once and when it was added more than once.
  • The time and space cost of add and find .
  • The trade-off between making add cheap and making find cheap, tied to the expected mix of calls.

Follow-up Questions Guidance

  • If find were called far more often than add , how would you redesign the class, and what would the change cost?
  • How would you support a remove(number) operation?
  • In Java or C++, what can go wrong when you compute value - number for targets near the 32-bit limits?
  • How would you make the class safe to call from several threads at once?