The fifteen patterns most coding interviews are built from, with the signal that gives each one away.
Read the signal, not the problem name. “Find the complement” is a hash map before it is Two Sum — that recognition is what is actually being tested.
Trade memory for time: one pass storing what you have seen turns a nested scan into a lookup.
time O(n)space O(n)
Implementation
var seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
int complement = target - nums[i];
if (seen.TryGetValue(complement, out int index))
return new[] { index, i };
seen[nums[i]] = i;
}
PractiseTwo Sum · Contains Duplicate · Group Anagrams · Valid Anagram · Longest Consecutive Sequence
About+
Coding interviews reuse a small number of shapes, so the skill being tested is recognising which one a problem is — not recalling a solution you have seen before.