Unit 4 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use two pointers to find two numbers that add up to the target by adjusting their positions based on the sum comparison to the target.
1) Initialize two pointers: `left` at the start of the list and `right` at the end.
2) While the `left` pointer is less than the `right` pointer:
a) Calculate the `current_sum` of the numbers at the two pointers.
b) If `current_sum` equals `target`, return the indices `[left, right]`.
c) If `current_sum` is less than `target`, increment the `left` pointer to try a larger sum.
d) If `current_sum` is greater than `target`, decrement the `right` pointer to try a smaller sum.
3) Since the problem guarantees a solution, the loop will always return before it terminates.
⚠️ Common Mistakes
def two_sum(nums, target):
left = 0
right = len(nums) - 1
while left < right:
current_sum = nums[left] + nums[right]
if current_sum == target:
return [left, right]
elif current_sum < target:
left += 1 # Need a larger sum
else:
right -= 1 # Need a smaller sum