Home › Interview Questions › Given an array arr[] of n integers and a target va…

Given an array arr[] of n integers and a target value, check if there exists a pair whose sum equals the target.

🟡 Medium Coding Junior level
1Times asked
Aug 2026Last seen
Aug 2026First seen

💡 Model Answer

A common solution is to use a hash set to store numbers seen so far. Iterate through the array; for each element x, compute y = target - x. If y is already in the set, a pair exists and you can return true. If not, add x to the set and continue. This runs in O(n) time and uses O(n) additional space. In Python:

python
def has_pair_with_sum(arr, target):
    seen = set()
    for num in arr:
        if target - num in seen:
            return True
        seen.add(num)
    return False

If you need to return the actual pair, store the numbers in a dictionary mapping value to index and retrieve the indices when a match is found.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500