Prepin
Log in

Identify all symmetric pairs within an array of pairs.

Approach / Explanation

The main goal of the question "Find all Symmetric Pairs in an Array of Pairs" is to evaluate your problem-solving abilities and your skill in using data structures like arrays and hash maps effectively. A symmetric pair in an array of pairs consists of two pairs, (a, b) and (c, d), that are defined as symmetric if a is equal to d and b is equal to c. In simpler terms, for a pair within the array, if there is another pair where the first element matches the second element of the current pair and the second element matches the first element of the current pair, then these pairs are symmetric. To tackle this question successfully: Recognize that you will be provided with an array of pairs. Understand that your objective is to identify all symmetric pairs. Plan to write a solution in a programming language that makes use of efficient data structures (like hash maps) or algorithms.

Suggested Answer

I will utilize a hash map to keep track of the pairs, enabling quick look-ups and effectively managing this task. Consider we have an array of pairs like [(1,2), (2,1), (3,4), (5,6), (6,5), (7,8)]. Here’s a Python implementation: def find_symmetric_pairs(arr): # Initialize an empty hash map to store pairs pair_dict = {} # Loop through the entire array for i in range(len(arr)): first = arr[i][0] second = arr[i][1] # Check if the second element of this pair exists in pair_dict # and if its value corresponds to the first element of this pair, # indicating a symmetric pair if (second in pair_dict and pair_dict[second] == first): print("(" + str(second) + ", " + str(first) + ")") else: pair_dict[first] = second find_symmetric_pairs([(1,2), (2,1), (3,4), (5,6), (6,5), (7,8)]) In the code provided, we traverse the array just once. For each pair, we check if its reverse is already present in our hash map; if it is, we print it out; otherwise, we add it to our hash map.

Alternative Answer

I would apply the same method as in example 1, but this time using JavaScript. Let's consider the pairs [(1,2), (3,4), (5,6), (6,5), (4,3), (2,1)]. Here’s a JavaScript implementation: function findSymmetricPairs(arr) { let pairMap = new Map(); for(let i = 0; i In this JavaScript code, we utilize a Map to keep track of pairs. We loop through the array, and for each pair, we check if its reverse is already in the Map. If it is, we print it. If not, we add the pair to the Map.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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