Two Pointers Problems for Coding Interviews
Two pointers turns a quadratic scan into a linear one by moving two indexes through the data with a rule for which one advances. It works on sorted arrays, on palindromes and when partitioning in place.
Decide what each pointer means and why the move is safe (it never skips a better answer). Practise the variants: opposite ends, same direction (fast and slow) and one pointer reading while the other writes.
Coding problems
- Valid Palindrome (Basic)
- Longest Palindromic Substring (Intermediate)
- Palindrome Partitioning Min Cuts (Advanced)
- Longest Palindrome From Letters (Basic)
- Count Palindromic Substrings (Intermediate)
- Minimum Insertions to Make Palindrome (Intermediate)
- Minimum Adjacent Swaps for Palindrome (Basic)
- Maximum Weight Cut (Basic)
- Longest Palindromic Subsequence (Intermediate)
- Palindrome Partitioning Min Cuts (Advanced)
- Shortest Palindrome (Advanced)
- BST Successor and Predecessor (Intermediate)
- Palindrome Linked List (Basic)
- Pseudo-Palindromic Root-to-Leaf Paths (Intermediate)
- Swap Kth Nodes From Both Ends (Intermediate)
- Count Distinct Palindromic Subsequences (Advanced)
- Count Palindromic Partitions (Intermediate)
- Palindrome Pairs Count (Advanced)
- Longest Palindrome by Concatenating Two-Letter Words (Intermediate)
- Rotate Image (Intermediate)
- Remove Duplicates Allowing Twice (Intermediate)
- Valid Palindrome With One Deletion (Basic)
- Palindrome Partitioning List (Intermediate)
- Manacher Palindrome Radii (Advanced)
- Prefix Sums of a Linked List (Basic)
- Reverse Even-Valued Runs (Intermediate)
- Longest Palindromic Sublist (Intermediate)
- Reverse Second Half of a Linked List (Basic)
- Flatten Binary Tree to Linked List (Intermediate)
- Count Proper K-Colorings (Advanced)
- Palindrome Removal (Advanced)
- Ways to Build Sturdy Brick Wall (Advanced)
- Count Palindromic Subsequences of Length 5 (Advanced)
- Unique Length-3 Palindromic Subsequences (Intermediate)
- Longest Chunked Palindrome Decomposition (Advanced)
- Palindrome Permutation Check (Basic)
- Break a Palindrome (Intermediate)
- Construct K Palindrome Strings (Intermediate)
- Can Make Palindrome From Substring (Intermediate)
- Longest Awesome Substring (Advanced)
- Sort Vowels in a String (Basic)
- Build Array From Permutation (Basic)
- Sort Transformed Array (Intermediate)
- Largest Palindromic Number (Intermediate)
- Minimum Characters to Append for Palindrome (Intermediate)
- Count Palindromic Times in Interval (Basic)
- Minimum Changes to Make Palindrome (Basic)
- Count Palindromic Rearrangements (Advanced)
- Chromatic Number of Small Graph (Advanced)
- Perfect Shuffle Cycle Length (Advanced)
- Palindrome Array Cost (Basic)
- Double-Base Palindromes Sum (Intermediate)
- Smallest Base Where Number Is Palindrome (Intermediate)
- Heap Build Swap Count (Intermediate)
- Largest Palindrome Product of Two N-Digit Numbers (Intermediate)
- Palindromic Clock Times in a Range (Basic)
- Palindromic Words Count (Basic)
- Palindromic Rows Count (Basic)
- Palindrome in a Given Base (Basic)
- Smallest Base Making a Palindrome (Intermediate)
Related
- Sliding window problems
- Dynamic programming problems
- Graph problems
- Practice coding problems online
- All interview question topics
Frequently asked questions
How do I get good at two pointers problems?
Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.
Can I run two pointers problems online here?
Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.