two pointers
two pointers is one of the most repeated interview dimensions. Start with edge-safe fundamentals, then move into pattern-level trade-offs.
Interview Signal
Frequently tests problem modeling, edge handling, and verbal clarity.
Common Pitfall
Template-only answers break under follow-up questioning.
Practice Strategy
Practice in 3-5 problem rounds and always review complexity alternatives.
Recommended Progression
Longest Palindromic Substring
Find the longest contiguous palindromic substring in a given string using dynamic programming and two-pointer expansion …
Container With Most Water
Find two vertical lines that can form a container with the most water in a given array of heights.
3Sum
Given an integer array, return all unique triplets where the sum is zero using two-pointer scanning with careful duplica…
3Sum Closest
Find the sum of three integers in an array that is closest to a given target using two-pointer scanning.
4Sum
The 4Sum problem requires finding all unique quadruplets in an array that sum to a specific target value.
Remove Nth Node From End of List
Remove the nth node from the end of a linked list using a two-pointer approach to solve efficiently.
Remove Duplicates from Sorted Array
Learn how to remove duplicates from a sorted array in-place using two-pointer scanning while preserving element order.
Remove Element
Remove Element challenges you to remove a value from an array in-place using efficient two-pointer scanning and tracking…
Find the Index of the First Occurrence in a String
Locate the first occurrence of a substring within a string using a two-pointer scanning strategy and invariant tracking …
Next Permutation
Next Permutation is a medium-difficulty problem focusing on generating the next lexicographically greater permutation of…
Trapping Rain Water
Calculate the total trapped rain water using the elevation map array, leveraging dynamic programming and two-pointer pat…
Rotate List
Rotate a singly linked list to the right by k positions using careful pointer manipulation and two-pointer traversal tec…
Sort Colors
Sort Colors requires in-place reordering of an array using a two-pointer scanning strategy to group 0s, 1s, and 2s effic…
Remove Duplicates from Sorted Array II
Solve the problem of removing duplicates from a sorted array in-place, ensuring each element appears at most twice, usin…
Remove Duplicates from Sorted List II
Remove duplicates from a sorted linked list, leaving only distinct values, and return the modified list in sorted order.
Partition List
Partition a linked list such that all nodes less than x come before nodes greater than or equal to x while preserving re…
Merge Sorted Array
Merge two sorted arrays into the first array in non-decreasing order, using a two-pointer approach.
Valid Palindrome
Check if a given string is a valid palindrome by using two-pointer scanning and invariant tracking techniques.
Linked List Cycle
Determine if a given linked list contains a cycle using pointer manipulation or hashing, focusing on detecting repeated …
Linked List Cycle II
Identify the start of a cycle in a linked list using pointer manipulation, efficiently handling edge cases without modif…
Reorder List
Reorder List requires careful pointer manipulation in a singly linked list to interleave nodes from the ends without alt…
Sort List
Sort List requires sorting a singly linked list efficiently using pointer manipulation and merge sort for optimal perfor…
Reverse Words in a String
Reverse Words in a String requires reordering words using precise two-pointer scanning with careful space handling for e…
Intersection of Two Linked Lists
Given two linked lists, find the node where they intersect or return null if they do not.
Compare Version Numbers
Compare two version numbers by checking their individual revisions, considering missing revisions as zero, using a two-p…
Two Sum II - Input Array Is Sorted
Solve the Two Sum II problem efficiently using binary search over a valid answer space in a sorted array.
Rotate Array
Rotate Array challenges you to shift elements right by k steps using precise two-pointer scanning and invariant tracking…
Happy Number
Determine if a number is happy by repeatedly summing squares of digits using two-pointer scanning to detect cycles effic…
Palindrome Linked List
Solve Palindrome Linked List by finding the midpoint, reversing the second half, and comparing mirrored nodes in linear …
Move Zeroes
Move all zeros in an array to the end while preserving the order of non-zero elements using efficient in-place operation…
Find the Duplicate Number
The problem involves finding the duplicate number in an array of integers, using a binary search approach over the valid…
Find Median from Data Stream
Implement a MedianFinder class that supports adding numbers and finding the median from a data stream.
Create Maximum Number
Create Maximum Number involves merging digits from two arrays while preserving order, maximizing the resulting number.
Reverse String
Reverse a character array in-place using a two-pointer scanning technique, ensuring minimal memory and correct index swa…
Reverse Vowels of a String
Reverse Vowels of a String uses a two-pointer approach to reverse the vowels in a string while leaving the consonants in…
Intersection of Two Arrays
Find the intersection of two arrays while ensuring unique elements using efficient array scanning and hash lookups.
Intersection of Two Arrays II
Find the intersection of two arrays, accounting for multiple occurrences of the same number.
Is Subsequence
Determine if one string is a subsequence of another using state transition dynamic programming for accurate character ma…
String Compression
Compress a character array in-place by converting consecutive repeated characters into counts using two-pointer scanning…
Assign Cookies
Maximize content children by assigning at most one cookie per child using two-pointer scanning and greedy sorting techni…
Circular Array Loop
Detect whether a circular array contains a loop of consistent direction using efficient array scanning and hash lookup t…
Heaters
Determine the minimum heater radius to cover all houses using a binary search over potential radius values efficiently.
Magical String
Count the number of '1's in the first n characters of a magical string using two-pointer scanning and invariant tracking…
Longest Uncommon Subsequence II
Find the longest string in an array that is not a subsequence of any other string, using array scanning and hash checks …
Longest Word in Dictionary through Deleting
Find the longest word in the dictionary that can be formed by deleting characters from a string, using two-pointer scann…
K-diff Pairs in an Array
Solve K-diff Pairs in an Array by counting unique differences with a hash map or sorted two-pointer sweep.
Reverse String II
Reverse String II requires reversing segments of a string using two-pointer scanning while tracking invariants carefully…
Next Greater Element III
Find the next greater integer using the same digits as a given number with precise two-pointer scanning and invariant tr…
Reverse Words in a String III
Reverse Words in a String III involves reversing each word in a sentence using the two-pointer technique.
Permutation in String
Check if a string contains a permutation of another string using two-pointer scanning and hash table techniques.
Shortest Unsorted Continuous Subarray
Find the shortest unsorted continuous subarray that, if sorted, would sort the entire array.
Valid Triangle Number
Count all triplets in an integer array that satisfy the triangle inequality to form valid triangles efficiently using so…
Sum of Square Numbers
Given a non-negative integer c, determine if there are two integers whose squares sum to c.
Palindromic Substrings
Count all palindromic substrings in a given string using state transition dynamic programming for efficient evaluation.
Two Sum IV - Input is a BST
Determine if a binary search tree contains two nodes whose values sum to a target using efficient traversal and state tr…
Find K Closest Elements
Identify the k integers closest to a target x in a sorted array using binary search and two-pointer strategies efficient…
Valid Palindrome II
Check if a string can become a palindrome by deleting at most one character using two-pointer scanning and invariant tra…
Count Binary Substrings
Count Binary Substrings solves for the number of valid substrings with equal 0's and 1's, grouped consecutively in a bin…
Find K-th Smallest Pair Distance
Solve Find K-th Smallest Pair Distance by sorting nums, then binary searching the distance and counting valid pairs with…
Partition Labels
Partition a string into maximal parts so that each letter appears in only one segment, using two-pointer scanning.
Swap Adjacent in LR String
Transform one string into another by swapping adjacent 'L' and 'R' characters in a sequence of moves.
K-th Smallest Prime Fraction
Find the k-th smallest fraction from a sorted array of unique primes using a binary search over the answer space.
Number of Subarrays with Bounded Maximum
Count the number of contiguous subarrays with a bounded maximum value using a two-pointer approach.
Expressive Words
Expressive Words challenges you to determine if a word can be transformed into a given string by extending groups of rep…
Shortest Distance to a Character
Compute the minimum distance from each character in a string to a target character using efficient scanning techniques.
Friends Of Appropriate Ages
The Friends Of Appropriate Ages problem involves counting valid friend requests between people based on their ages with …
Most Profit Assigning Work
Assign workers to jobs maximizing total profit using difficulty, profit, and worker arrays efficiently with binary searc…
Flipping an Image
Flip each row of a binary matrix horizontally and invert its values efficiently using a two-pointer scanning approach.
Push Dominoes
In the "Push Dominoes" problem, you simulate the falling dominoes based on their initial states and determine their fina…
Backspace String Compare
Compare two strings after processing backspaces using efficient two-pointer scanning and careful invariant tracking to e…
Longest Mountain in Array
Find the length of the longest subarray forming a mountain pattern using state transitions and two-pointer logic efficie…
Advantage Shuffle
Maximize the advantage of nums1 over nums2 using a two-pointer greedy strategy, carefully tracking which elements beat o…
Middle of the Linked List
Find the middle node of a singly linked list using a two-pointer technique.
Boats to Save People
Find the minimum number of boats required to save all people, using a two-pointer approach for efficient pairing.
Sort Array By Parity
Reorder an integer array so all even numbers come before odd numbers using a precise two-pointer scanning method.
Reverse Only Letters
Reverse the characters of a string while skipping non-letter characters using a two-pointer technique.
Sort Array By Parity II
Sort an array where even numbers appear at even indices and odd numbers appear at odd indices using two-pointer scanning…
3Sum With Multiplicity
Count all unique triplets in an integer array whose sum equals the target, handling multiplicity efficiently using hashi…
Long Pressed Name
Check if a typed string could result from long pressing characters while typing a given name using a two-pointer scan.
DI String Match
Reconstruct a permutation from a DI string using two-pointer scanning, carefully tracking the increasing and decreasing …
Bag of Tokens
Maximize your score in the Bag of Tokens problem by strategically playing tokens with two-pointer scanning and greedy te…
Maximum Width Ramp
Find the maximum width of a ramp where nums[i] <= nums[j] for i < j using a two-pointer approach.
Pancake Sorting
Sort an array using pancake flips, leveraging two-pointer scanning and invariant tracking to iteratively position the la…
Squares of a Sorted Array
The problem involves squaring elements of a sorted array and returning the squares in non-decreasing order.
Interval List Intersections
This problem requires finding the intersection of two lists of intervals using a two-pointer technique with invariant tr…
Camelcase Matching
Camelcase Matching is a medium difficulty problem where you match queries to a given pattern by inserting lowercase lett…
Longest String Chain
Find the longest word chain by scanning arrays and using hash lookups to efficiently track predecessor-successor sequenc…
Duplicate Zeros
Duplicate each zero in a fixed-length array in place, shifting elements right using a two-pointer scanning approach effi…
Longest Chunked Palindrome Decomposition
Solve the "Longest Chunked Palindrome Decomposition" problem by using dynamic programming and string manipulation techni…
Last Substring in Lexicographical Order
Identify the lexicographically last substring in a string using two-pointer scanning with invariant tracking efficiently…
Find Positive Integer Solution for a Given Equation
Determine all positive integer pairs that satisfy a hidden monotonic function for a given target using binary search ove…
Remove Palindromic Subsequences
This problem challenges you to remove palindromic subsequences from a string with a minimum number of steps using two-po…
Check If N and Its Double Exist
Solve Check If N and Its Double Exist by scanning once and checking doubles or halves in a hash set.
Find the Distance Value Between Two Arrays
Calculate the distance value between two integer arrays by determining how many elements in arr1 don't have a close coun…
Check If a Word Occurs As a Prefix of Any Word in a Sentence
Determine the first word in a sentence where a given searchWord is a prefix using two-pointer scanning with string match…
The k Strongest Values in an Array
Identify the k strongest values in an array using two-pointer scanning and careful tracking of the array's centre value.
Number of Subsequences That Satisfy the Given Sum Condition
Count all non-empty subsequences in an integer array where the sum of the minimum and maximum elements is at most a targ…
Range Sum of Sorted Subarray Sums
Compute the sum of sorted subarray sums efficiently using binary search over valid sum ranges and prefix sum accumulatio…
Get the Maximum Score
Find the maximum possible score from two sorted arrays with a dynamic programming approach, leveraging partitioning and …
Shortest Subarray to be Removed to Make Array Sorted
Find the shortest subarray to remove in order to make an array non-decreasing using binary search and two-pointer techni…
Number of Ways Where Square of Number Is Equal to Product of Two Numbers
Find the number of triplets where the square of a number equals the product of two others, utilizing array scanning and …
Split Two Strings to Make Palindrome
Determine if splitting two equal-length strings at a common index can form a palindrome using two-pointer scanning techn…
Max Number of K-Sum Pairs
Max Number of K-Sum Pairs is a problem involving counting disjoint pairs with a given sum k in an array.
Checking Existence of Edge Length Limited Paths
Solve the problem of checking if there exists a path between two nodes under edge length constraints using efficient tec…
Ways to Split Array Into Three Subarrays
The problem requires finding the number of ways to split an array into three subarrays where each split meets a certain …
Swapping Nodes in a Linked List
Swap nodes in a linked list using linked-list pointer manipulation and two pointers to solve this medium difficulty prob…
Minimum Length of String After Deleting Similar Ends
Minimize string length by deleting similar characters from both ends repeatedly using a two-pointer technique.
Largest Merge Of Two Strings
Construct the lexicographically largest merge from two strings using a two-pointer greedy scanning approach efficiently.
Closest Subsequence Sum
Find the minimum absolute difference between a target goal and any subsequence sum using optimized dynamic programming a…
Form Array by Concatenating Subarrays of Another Array
Determine if you can sequentially select disjoint subarrays from nums matching each group in order using two-pointer sca…
Merge Strings Alternately
Merge two strings alternately, starting with the first string, and append extra characters when one string is longer.
Count Pairs Of Nodes
Given a graph with nodes and edges, count pairs of nodes where the degree sum exceeds a given threshold for each query.
Maximum Score of a Good Subarray
Maximize the score of a good subarray using binary search to explore the valid answer space with a focus on two-pointer …
Sentence Similarity III
Sentence Similarity III asks if one sentence can be transformed into another by inserting a sentence inside it.
Minimum Adjacent Swaps to Reach the Kth Smallest Number
Find the minimum number of adjacent swaps to reach the kth smallest wonderful integer from a given number string.
Maximum Distance Between a Pair of Values
Find the maximum distance between a pair of values in two non-increasing integer arrays using binary search.
Rotating the Box
Rotate a box represented by a character matrix, letting stones fall under gravity using precise two-pointer scanning and…
Minimize Maximum Pair Sum in Array
Minimize the maximum pair sum in an array by optimally pairing its elements.
Maximum Number of Removable Characters
Use binary search and a subsequence check to find the largest removable prefix that still keeps p inside s.
Check If String Is a Prefix of Array
Determine if a string is a prefix of an array by concatenating initial elements, using two-pointer scanning efficiently.
Minimum Number of Swaps to Make the String Balanced
Determine the minimum swaps to balance a bracket string using two-pointer scanning with invariant tracking efficiently.
Reverse Prefix of Word
Reverse the prefix of a string starting from index 0 to the first occurrence of a given character.
Partition Array Into Two Arrays to Minimize Sum Difference
Partition an integer array into two equal halves to minimize the absolute difference of their sums using dynamic program…
Maximum Number of Tasks You Can Assign
Maximize the number of tasks that can be completed by efficiently using workers and magical pills.
Delete the Middle Node of a Linked List
Efficiently remove the middle node from a linked list using two-pointer techniques and careful pointer updates to mainta…
Watering Plants II
Simulate watering a row of plants with Alice and Bob using two-pointer scanning, tracking refills precisely for each ste…
Find First Palindromic String in the Array
Return the first palindromic string from an array of words or an empty string if none exists.
Adding Spaces to a String
Learn to efficiently insert spaces in a string using two-pointer scanning with invariant tracking in linear time.
Recover the Original Array
Recover the Original Array helps you understand how to reverse-engineer an array from two subsets using array scanning a…
Maximum Twin Sum of a Linked List
Find the maximum twin sum in a linked list by manipulating pointers efficiently and leveraging stack or reversal techniq…
Rearrange Array Elements by Sign
Rearrange an array by alternating positive and negative integers using a two-pointer approach with invariant tracking.
Partition Array According to Given Pivot
Rearrange an array around a pivot while maintaining relative order using a two-pointer scanning approach efficiently.
Minimum Number of Moves to Make Palindrome
The problem challenges you to find the minimum number of adjacent swaps to make a string a palindrome.
Find All K-Distant Indices in an Array
Identify all indices in an array that are within k distance of elements equal to the given key using two-pointer scannin…
Maximum Total Beauty of the Gardens
Determine the maximum total beauty of gardens by strategically planting flowers using binary search over achievable flow…
Successful Pairs of Spells and Potions
Count how many potions pair successfully with each spell using binary search over sorted potion strengths efficiently.
The Latest Time to Catch a Bus
Find the latest time to catch a bus based on departure times, passenger arrivals, and capacity using binary search over …
Move Pieces to Obtain a String
Determine if the pieces in start can be moved to form target using two-pointer scanning and strict left-right movement r…
Number of Arithmetic Triplets
Count the number of arithmetic triplets in a given strictly increasing array with a specified difference.
Strictly Palindromic Number
Determine if a number is strictly palindromic in all bases from 2 to n minus 2 using two-pointer scanning and invariant …
Divide Intervals Into Minimum Number of Groups
Determine the minimum number of non-overlapping groups for a set of intervals using precise two-pointer scanning logic.
Maximum Matching of Players With Trainers
Maximize the number of valid player-trainer matchings using two-pointer scanning and careful sorting strategy.
Largest Positive Integer That Exists With Its Negative
Find the largest positive integer in an array such that its negative counterpart also exists.
Apply Operations to an Array
Transform an array by applying adjacent doubling operations and shifting zeros efficiently using two-pointer scanning te…
Total Cost to Hire K Workers
Optimize the total cost of hiring exactly k workers using a two-pointer approach with invariant tracking and priority qu…
Number of Distinct Averages
Calculate the number of unique averages formed by repeatedly pairing smallest and largest elements efficiently using has…
Maximum Number of Non-overlapping Palindrome Substrings
Find the maximum number of non-overlapping palindromic substrings of at least length k in a string using dynamic program…
Append Characters to String to Make Subsequence
Determine the minimum characters to append to s so t becomes a subsequence using efficient two-pointer scanning techniqu…
Divide Players Into Teams of Equal Skill
Divide players into equal skill teams and return the sum of their chemistry, or -1 if impossible.
Maximum Number of Points From Grid Queries
Solve the Maximum Number of Points From Grid Queries problem using two-pointer scanning and invariant tracking.
Maximum Enemy Forts That Can Be Captured
Find the maximum number of enemy forts that can be captured by moving your army using two-pointer scanning with invarian…
Minimum Common Value
Find the smallest integer appearing in both sorted arrays efficiently using array scanning and hash-based lookup techniq…
Find the Array Concatenation Value
Compute the array concatenation value efficiently using two-pointer scanning while maintaining a running total of concat…
Count the Number of Fair Pairs
Efficiently count all fair pairs in an array using sorting and binary search, focusing on the sum range between lower an…
Subsequence With the Minimum Score
Find the minimum score of a string by removing characters from t while maintaining subsequence validity with s.
Merge Two 2D Arrays by Summing Values
Merge two 2D arrays by summing values with matching ids using array scanning and hash lookup efficiently.
Find the Maximum Number of Marked Indices
Maximize marked indices in an array by performing allowed operations, applying binary search to find the optimal count e…
Maximize Greatness of an Array
Maximize Greatness of an Array requires permuting numbers to exceed original values at most indices efficiently.
Lexicographically Smallest Palindrome
Given a string, make it a palindrome with the fewest operations, prioritizing lexicographically smallest result.
Count Pairs Whose Sum is Less than Target
This problem asks you to count all index pairs in an array whose sum is strictly less than a given target value efficien…
Make String a Subsequence Using Cyclic Increments
Determine if str2 can be made a subsequence of str1 by incrementing characters cyclically using at most one operation.
Minimum Array Length After Pair Removals
This problem involves minimizing the length of a sorted array by repeatedly removing adjacent pairs of equal elements.
Find Indices With Index and Value Difference I
Find two indices in an array where their difference in both index and value meets specified thresholds.
Find Indices With Index and Value Difference II
Locate two indices in an array where both index and value differences meet specified thresholds using precise scanning t…
Minimum Changes to Make K Semi-palindromes
Minimize the number of letter changes to partition a string into k semi-palindromes using dynamic programming and two po…
Separate Black and White Balls
Solve the "Separate Black and White Balls" problem by swapping adjacent balls to group all black balls to the right with…
Count the Number of Incremovable Subarrays I
Count the number of incremovable subarrays in an array by removing them to create a strictly increasing sequence.
Count the Number of Incremovable Subarrays II
Count the number of incremovable subarrays where removal of the subarray results in a strictly increasing array.
Find Beautiful Indices in the Given Array I
Identify all beautiful indices where substring a appears and a nearby substring b exists within distance k efficiently.
Find Beautiful Indices in the Given Array II
Find Beautiful Indices in the Given Array II challenges you to use binary search and string matching techniques.
Find the Integer Added to Array II
Given two arrays nums1 and nums2, determine the integer added to nums1 to make it equal to nums2 after removing two elem…
Maximum Total Damage With Spell Casting
Calculate the maximum total damage by selectively casting spells while avoiding adjacent power conflicts using array sca…
Minimum Average of Smallest and Largest Elements
Solve the Minimum Average of Smallest and Largest Elements problem using two-pointer scanning to track averages effectiv…
Minimum Number of Flips to Make Binary Grid Palindromic I
Find the minimum flips to make a binary grid palindromic by scanning with two pointers and tracking symmetry efficiently…
Minimum Number of Flips to Make Binary Grid Palindromic II
The problem asks to flip cells in a binary grid to make its rows and columns palindromic using the minimum number of fli…
Find the Lexicographically Smallest Valid Sequence
Determine the lexicographically smallest valid index sequence by using state transition dynamic programming over word1 a…
Find Maximum Removals From Source String
Determine the maximum number of characters you can remove from source while keeping pattern as a subsequence using array…
Find the Lexicographically Largest String From the Box I
This problem involves finding the lexicographically largest string from a given word using a two-pointer approach.
Shortest Matching Substring
Find the shortest substring in a string that matches a pattern with exactly two wildcards efficiently using binary searc…
Longest Palindrome After Substring Concatenation I
Compute the maximum palindrome length by concatenating substrings from two strings using state transition dynamic progra…
Longest Palindrome After Substring Concatenation II
Compute the longest palindrome by concatenating substrings from two strings using state transition dynamic programming e…
Maximum Product of First and Last Elements of a Subsequence
Maximize the product of the first and last elements of any subsequence of size m in an integer array.
Related Patterns
Two-pointer scanning with invariant tracking
99 linked problems
Binary search over the valid answer space
32 linked problems
Array scanning plus hash lookup
21 linked problems
State transition dynamic programming
15 linked problems
Linked-list pointer manipulation
14 linked problems
Binary-tree traversal and state tracking
1 linked problems