Two Sum
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole Mygate Two Sum question hinges on one data structure: a hash map. Candidates reported it in August 2024, and it's the classic version with a small twist. You return the two indices in increasing order, and you can't reuse the same position. If you've seen this one before, it's a five-minute win. If your brain freezes and the nested loop is all you can see, you're in trouble. If you blank on the live OA, StealthCoder is the safety net running invisibly on your screen.
The problem
Given an integer array nums and an integer target, return the indices of the two distinct elements whose sum equals target. Exactly one unordered pair of indices is a solution. Return its two zero-based indices in increasing order. The same array position cannot be used twice. Function twoSum(nums: int[], target: int) → int[] Examples Example 1 nums = [2,7,11,15] target = 9 return = [0,1] The values at indices 0 and 1 add to 9. Example 2 nums = [3,2,4] target = 6 return = [1,2] The two different positions containing 2 and 4 add to 6. Constraints 2 ≤ nums.length ≤ 10,000. −100,000,000 ≤ nums[i], target ≤ 100,000,000. Exactly one pair of distinct indices has the required sum.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is the complement. For each number x, you need target - x. Walk the array once and keep a hash map from value to index. Before inserting the current element, check whether its complement is already in the map. If it is, you've found the pair, and the stored index is always smaller than the current one, so the increasing order comes for free. Checking before inserting is what stops you from using the same position twice, which matters for cases like [3,3] with target 6. The common pitfall is the O(n^2) double loop. With n up to 10,000 it might pass, but it's not what they want. Another pitfall is returning values instead of indices. Negative numbers are fine since the map doesn't care about sign. Overflow isn't a concern with these bounds in most languages. If you freeze during the live OA, StealthCoder can hand you the one-pass map solution.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Two Sum cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as two sum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Mygate's OA.
Mygate reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Two Sum FAQ
How hard is the Mygate Two Sum OA really?+
It's easy. This is the standard hash map problem, reported at Mygate in August 2024. The only real work is returning indices in increasing order and not reusing an index. If you've written it once, you can write it in a few minutes.
What's the trick to solve Two Sum fast?+
Use a hash map from value to index. For each element, compute target minus the element and check the map. If it's there, return the stored index and the current one. If not, store the current value. One pass, O(n) time.
Why not just use two nested loops?+
It works on correctness since n is at most 10,000, but it's O(n^2) and signals you don't know the standard approach. Hidden tests or reviewers may prefer the linear solution. The map version is barely longer to write.
How do I handle duplicates like [3,3] with target 6?+
Check the map before inserting the current value. When you reach the second 3, the first 3 is already stored, so the complement is found at index 0. Inserting first would let one element match itself, which breaks the distinct-index rule.
How do I prepare for this in 48 hours?+
Write the hash map solution from memory two or three times. Test it on both examples plus a duplicate case and a negative number case. Then spend the rest of your time on harder hash map and array problems, since an OA usually has more than one question.