Arrays & Strings
Data Structures
Preview
Array patterns
Two pointers
# Reverse a list in place\ndef reverse(nums):\n left, right = 0, len(nums) - 1\n while left < right:\n nums[left], nums[right] = nums[right], nums[left]\n left += 1\n right -= 1\n return numsSliding window
# Maximum sum of any 3 consecutive numbers\ndef max_window_sum(nums, k):\n window = sum(nums[:k])\n best = window\n for i in range(k, len(nums)):\n window += nums[i] - nums[i - k]\n best = max(best, window)\n return bestString building
def is_palindrome(s: str) -> bool:\n s = s.lower()\n cleaned = "".join(c for c in s if c.isalnum())\n return cleaned == cleaned[::-1]Frequency counting
def most_frequent(nums):\n counts = {}\n for n in nums:\n counts[n] = counts.get(n, 0) + 1\n return max(counts, key=counts.get)The checklist
- Can you avoid nested loops with a hash map?
- Is the data sorted? Two pointers may work
- Do you need all subarrays, or just max/min? (window)
- Could you process from both ends?
Tip: A dict turns a lookup that costs O(n) into one that costs O(1). Most "can you do it in one pass?" answers use a map.This is a free preview lesson
Enroll in the Problem Solving & Algorithms With Python to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.