LeetCode 170, Easy. Topics: Design, Array, Hash Table, Two Pointers, Data Stream. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 17 parametrized pytest cases, and a playground notebook:
Problem
Design a data structure that accepts a stream of integers and checks if it has a pair of integers that sum up to a particular value.
Implement the TwoSum class:
TwoSum() Initializes the TwoSum object, with an empty array initially.
void add(int number) Adds number to the data structure.
boolean find(int value) Returns true if there exists any pair of numbers whose sum is equal to value, otherwise, it returns false.
Examples
Constraints
-10^5 <= number <= 10^5
-2^31 <= value <= 2^31 - 1
- At most
10^4 calls will be made to add and find.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026