Q1
Easy
Two Sum: Find two numbers in an array that add up to a target sum.
Use a Hash Map to record previously seen numbers and their indices, achieving O(n) time complexity.
Solution Code
def two_sum(nums, target):
seen = {} # value -> index
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []
# Test: nums = [2, 7, 11, 15], target = 9 -> [0, 1]