How do you approach the Two Sum problem in Python, and why is the hash map version better than brute force?
Two Sum gives you a list of numbers and a target, and it asks for the indices of the two values that add up to that target. The brute-force answer tries every possible pair and costs O(n squared) time. The better answer keeps a dictionary of the values it has already seen, and it costs O(n) time. The earlier questions in this topic covered lists and dictionaries, and this problem puts both of them to work.
The reasoning behind the dictionary version is the part that you should explain rather than only showing the code. For each value that you look at, you can work out the value that would complete the pair, which is the target minus the current value. If that complement has already been seen, then you have found the pair, and the dictionary hands you its index directly. Otherwise you record the current value and its index, and you move on to the next one. Each step of that is a single hash lookup, so the whole pass is linear.
Two small details make the difference between a correct answer and a subtly wrong one. The lookup happens before the insert, so an element is never paired with itself, because the target 4 against the single value 4 would otherwise wrongly succeed. And you store the index rather than the value, because the question asks for indices and not for the numbers themselves.
When duplicates exist, the dictionary approach naturally returns the first valid pair, which is what people usually expect. If the interviewer wants every possible pair instead, that is a different problem, and the same two-sum idea still works as a starting point for it.