Skip to content

Nozibul/Two_pointer-coding-pattern-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Two Pointer Coding Pattern - Ultimate Resource Guide 🎯

Table of Contents

Overview

Master the Two Pointer Pattern with this comprehensive PDF guide! Learn to identify the right problems in seconds and streamline your coding solutions. Enhance your algorithmic thinking today. The Two Pointer technique is a fundamental algorithmic pattern used to solve array and string problems efficiently. Instead of using nested loops (O(nΒ²)), this technique reduces time complexity to O(n) by using two pointers that traverse the data structure strategically.

Key Concepts

When to Use Two Pointers:

  • Array/String manipulation problems
  • Searching pairs with specific conditions
  • Palindrome checking
  • Sliding window problems
  • Merge operations on sorted arrays

Types of Two Pointer Approaches:

  1. Opposite Direction - Start from both ends, move towards center
  2. Same Direction - Both pointers start from same position
  3. Fast & Slow - Different speeds for cycle detection

Learning Resources

πŸ† Premium Educational Platforms

1. Educative.io - Grokking the Coding Interview

  • Link: Educative Two Pointers Pattern
  • What's Great: Interactive visual explanations, structured learning path
  • Best For: Systematic interview preparation

πŸ“š Free Comprehensive Tutorials

2. GeeksforGeeks - Two Pointers Technique

  • Link: GfG Two Pointers Guide
  • What's Great: Detailed explanations, multiple examples, code implementations
  • Best For: Understanding fundamentals and basic implementations
  • Cost: Free

3. Medium Articles (Curated)

Essential Reads:

  • "LeetCode is Easy! The Two Pointer Pattern" by Tim Park

    • Recognition techniques and pattern variations
    • Problem classification strategies
  • "Master the Two Pointer Technique" by John Kamau

    • Time complexity analysis and optimization techniques
    • Real-world problem examples

πŸ’» Practice Platforms

5. LeetCode - Two Pointers Topic

  • Link: LeetCode Two Pointers Problems
  • Problems Count: 100+ curated problems
  • Difficulty: Easy to Hard
  • Features: Community solutions, discussion forums

6. HackerRank - Arrays and Strings

  • Link: HackerRank Practice
  • What's Great: Gradual difficulty progression
  • Best For: Building confidence through structured practice

Practice Problems

🟒 Beginner Level

Problem Platform Difficulty Pattern Type
Two Sum LeetCode #1 Easy Opposite Direction
Valid Palindrome LeetCode #125 Easy Opposite Direction
Remove Duplicates LeetCode #26 Easy Same Direction

🟑 Intermediate Level

Problem Platform Difficulty Pattern Type
3Sum LeetCode #15 Medium Opposite Direction
Container With Most Water LeetCode #11 Medium Opposite Direction
Longest Substring Without Repeating LeetCode #3 Medium Sliding Window

πŸ”΄ Advanced Level

Problem Platform Difficulty Pattern Type
Trapping Rain Water LeetCode #42 Hard Opposite Direction
Minimum Window Substring LeetCode #76 Hard Sliding Window
Linked List Cycle II LeetCode #142 Medium Fast & Slow

Implementation Examples

Pattern 1: Opposite Direction Pointers

def two_sum_sorted(arr, target):
    left, right = 0, len(arr) - 1
    
    while left < right:
        current_sum = arr[left] + arr[right]
        
        if current_sum == target:
            return [left, right]
        elif current_sum < target:
            left += 1
        else:
            right -= 1
    
    return [-1, -1]

Pattern 2: Same Direction Pointers

def remove_duplicates(arr):
    if not arr:
        return 0
    
    write_index = 1
    
    for read_index in range(1, len(arr)):
        if arr[read_index] != arr[read_index - 1]:
            arr[write_index] = arr[read_index]
            write_index += 1
    
    return write_index

Pattern 3: Fast & Slow Pointers

def has_cycle(head):
    if not head:
        return False
    
    slow = fast = head
    
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        
        if slow == fast:
            return True
    
    return False

Time Complexity Analysis

Approach Time Complexity Space Complexity Use Case
Brute Force O(nΒ²) O(1) Simple nested loops
Two Pointers O(n) O(1) Optimized linear scan
Hash Map O(n) O(n) When two pointers not applicable

Common Patterns

🎯 Pattern Recognition Guide

Use Two Pointers When:

  • Array/string is sorted or can be sorted
  • Looking for pairs/triplets with specific sum
  • Need to reverse or palindrome check
  • Sliding window maximum/minimum problems
  • Merge two sorted sequences

Avoid Two Pointers When:

  • Need to track multiple elements simultaneously
  • Random access patterns required
  • Complex state management needed

Contributing

Found a great resource or want to add more examples?

How to Contribute:

  1. Fork this repository
  2. Create a feature branch (git checkout -b feature/new-resource)
  3. Add your resource with proper description and links
  4. Test all links and verify content quality
  5. Submit a pull request with detailed description

Contribution Guidelines:

  • Verify resource quality and accuracy
  • Include brief description of what makes it valuable
  • Categorize appropriately (beginner/intermediate/advanced)
  • Follow existing markdown formatting

πŸ“ˆ Learning Path Recommendation

Week 1-2: Foundation

  1. Read GeeksforGeeks comprehensive guide
  2. Understand basic patterns and implementations
  3. Solve 10-15 easy problems on LeetCode

Week 3-4: Pattern Mastery

  1. Study Educative.io interactive course
  2. Focus on different pattern variations
  3. Solve 20-30 medium problems

Week 5-6: Advanced Applications

  1. Review USACO competitive programming guide
  2. Read Medium articles for deeper insights
  3. Tackle hard problems and optimization challenges

πŸ“ž Questions or Suggestions?

Feel free to open an issue or reach out if you:

  • Have questions about specific problems
  • Want to suggest additional resources
  • Found broken links or outdated information
  • Need clarification on any pattern or concept

Happy Coding! πŸš€


Last Updated: 9/3/2025
Maintained by: Nozibul Islam

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages