Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/Python/Coding/Find pairs with sum X from a list of numbers

Find pairs with sum X from a list of numbers

Python/Codingeasy2 min read

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…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
179
questions in Python/Coding
Difficulty Split
127E|24M|28H
in this category
Total Bank
1,863
across 7 categories

Why This Question Matters

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.

How to Approach This

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.

Expert Answer
425 wordsIncludes code

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.

Mechanics and Why

For each number 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).

Handling Duplicates and Example

The exact handling of duplicates depends on whether you need to count distinct pairs (e.g., (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

For counting all possible pairs, including multiple occurrences of the same pair (e.g., [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:
If num == complement, add count(num) (count(num) - 1) // 2 to the total.
If 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).

In the interview, also mention…

For very large datasets that don't fit into memory, this problem shifts from a single-machine O(n) to a distributed computing challenge. In systems like Apache Spark, processing would involve distributing the data. A naive hash set approach would require collecting data to a single node, which is infeasible. Alternatives might include sorting the entire dataset (which involves significant shuffle operations in distributed systems) and then using a two-pointer approach, resulting in O(N log N) overall. Also, consider potential integer overflow if X or num are extremely large in languages with fixed-size integers.
⚡
Pro Tip

Pro-Move: Counter for duplicate pairs. Red Flag: O(n²) nested loop.

Want all answers as a PDF for offline study?
Seven focused volumes with 750+ in-depth answers — Answer Vault →

Related Python/Coding Questions

easyWhat are traits in Scala, and how are they different from classes?FreemediumWrite a Python function to check if a string is a palindrome.FreeeasyWhat is the difference between a list and a tuple in Python?FreeeasyExplain the difference between shallow copy and deep copy in Python.FreeeasyWrite a Python function to find the first non-repeating character in a string.Free

Level up your prep

Recommended
Educative
Educative Unlimited

800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

Start learning →

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.

← Back to all questionsMore Python/Coding questions →
Categories
All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
By Company
AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
Interview Guides
All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
Products
AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer