Reviewed by Aditya Kumar · Last reviewed 2026-03-24
The most efficient approach to find pairs with sum X from a list of numbers is using a hash set (or dictionary in Python). This allows for an average case time complexity of O(n) by performing a…
This easy-level Python/Coding question appears frequently in data engineering interviews at companies like Paytm. While less common, it tests deeper understanding that distinguishes strong candidates.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
The most efficient approach to find pairs with sum X from a list of numbers is using a hash set (or dictionary in Python). This allows for an average-case time complexity of O(n) by performing a single pass through the list.
num in the input list, we calculate its complement = X - num. We then check if this complement already exists in our seen hash set. If it does, we've found a pair (num, complement) that sums to X. After checking, we add num to the seen hash set. The key advantage of a hash set is its average O(1) time complexity for insertion and lookup operations. This makes the overall process proportional to the number of elements n in the list, hence O(n).
(2,3) and (3,2) are considered the same) or all possible pairs (e.g., from [2,2,3,3] for X=5, you'd count two (2,3) pairs).
For distinct pairs:
def find_distinct_pairs_with_sum(nums, target_sum):
seen = set()
found_pairs = set() # Stores unique pairs as sorted tuples
for num in nums:
complement = target_sum - num
if complement in seen:
# Store sorted tuple to treat (2,3) and (3,2) as the same
found_pairs.add(tuple(sorted((num, complement))))
seen.add(num)
return len(found_pairs) # Or list(found_pairs) if the actual pairs are needed
[2,2,3,3] for X=5 yields two (2,3) pairs), you would first build a frequency map (like collections.Counter in Python) of all numbers. Then, iterate through the unique numbers in the frequency map:num == complement, add count(num) (count(num) - 1) // 2 to the total.num != complement, and complement exists in the map, add count(num) count(complement) to the total, ensuring to process each (num, complement) pair only once (e.g., by only considering num < complement).
X or num are extremely large in languages with fixed-size integers.Pro-Move: Counter for duplicate pairs. Red Flag: O(n²) nested loop.
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.