Prepin
Log in

Given an unsorted array of integers, identify a pair that sums up to the specified value.

Approach / Explanation

This question requires you to create an algorithm to check if there are two numbers in an array that sum up to a specified value. Here’s a summary of what you need to accomplish: Unordered array of integers - This is your input. The array contains integers in any random order, and you cannot assume any specific properties regarding their arrangement or distribution. Identify a pair that sums to the target - Your objective is to find out if there exist two numbers in the array that, when added together, equal a specified target sum. Important considerations to keep in mind: What is the array size? - For a small array, a brute force method can be used, where each element is compared with every other element. This approach results in a time complexity of O(n^2). What about space complexity? - If space limitations are not a concern, a hashing technique can be employed, which would lower the time complexity to O(n) but would require additional space. Do the pairs need to be distinct? - The prompt does not clarify this, so it's advisable to confirm this with your interviewer.

Suggested Answer

When the input size is relatively small, we can implement a straightforward nested loop to check every pair: def find_sum_pair ( array , target_sum ) : for i in range ( len ( array ) ) : for j in range ( i + 1 , len ( array ) ) : if array [ i ] + array [ j ] == target_sum : return ( array [ i ] , array [ j ] ) return None In this method, we traverse the array and for each element, we search for its complement (i.e., target_sum - array[i]). If we cannot locate a pair that sums to the target value, we return None to signify that no such pair exists. This approach is simple but not efficient for larger arrays due to its O(n^2) time complexity.

Alternative Answer

When dealing with large inputs, it's essential to find a more efficient approach. In this case, we can utilize a technique known as 'hashing'. def find_sum_pair ( array , target_sum ) : hash_table = { } for i in range ( len ( array ) ) : complement = target_sum - array [ i ] if complement in hash_table : return ( complement , array [ i ] ) hash_table [ array [ i ] ] = i return None In this approach, we initiate an empty hash table and then loop through the array. For each item, we compute its complement and check for its existence in the hash table. If it is present, we have identified a pair and return it. If not, we record the current element along with its index in the hash table to facilitate checks against subsequent elements. This technique lowers the time complexity to O(n), although it requires additional space (O(n) space complexity) for the hash table.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Amazon

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.