Intermediate·Concept·2 min read

Sliding window and two pointers in Python

Practice this question

Problems to practice

  • Longest Substring Without Repeating Characters
  • Maximum Sum Subarray of Size K
  • Minimum Window Substring
  • Container With Most Water
  • Valid Palindrome

Example: two pointers palindrome

Python
def is_palindrome(s):
    left, right = 0, len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True

Adapted from 18_DSA_PYTHON.md · “12. Sliding window and two pointers in Python”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.