Two Pointers
Everything on SkillVeris tagged Two Pointers — collected across the glossary, study notes, blog, and cheat sheets.
14 resources across 1 library
Interview Questions(14)
What is the Two Pointer Technique?
The two pointer technique uses two index variables that move through a data structure — typically a sorted array or a linked list — to solve problems in O(n) t…
What is the Sliding Window Technique?
The sliding window technique maintains a contiguous subrange of an array or string, expanding and contracting its boundaries as it scans, to solve subarray or…
How Do You Solve the Trapping Rain Water Problem?
The Trapping Rain Water problem, given an array of bar heights, is solved optimally with a two-pointer approach in O(n) time and O(1) space: water trapped abov…
Minimum Window Substring: How Do You Solve It?
Minimum window substring is solved with a variable-size sliding window and two hash maps: expand the right pointer until the window contains every required cha…
Longest Substring Without Repeating Characters
The longest substring without repeating characters is found with a sliding window and a hash map that stores the last seen index of each character: expand the…
How Do You Detect a Cycle in a Linked List?
You detect a cycle in a linked list by walking it with two pointers moving at different speeds — a slow pointer advancing one node at a time and a fast pointer…
What is Floyd’s Cycle Detection Algorithm?
Floyd’s cycle detection algorithm, also called the tortoise-and-hare algorithm, finds whether a linked structure contains a cycle and, with a second phase, loc…
How Do You Find the Middle of a Linked List?
You find the middle of a linked list in a single pass using the slow-fast pointer technique: advance a slow pointer one node at a time and a fast pointer two n…
How Do You Rotate an Array by K Positions?
The optimal way to rotate an array of n elements right by k positions in place is the three-reversal trick: reverse the whole array, then reverse the first k e…
How Do You Find the Longest Palindromic Substring?
The expand-around-center approach finds the longest palindromic substring in O(n²) time and O(1) extra space by treating every index (and every gap between ind…
What is the Container With Most Water Problem?
The container with most water problem finds two vertical lines from an array of heights that, together with the x-axis, form the container holding the most wat…
How Do You Solve the Three Sum Problem?
Sort the array, fix one element at a time, then use a two-pointer sweep on the remaining subarray to find pairs that sum to the negative of the fixed element,…
How Do You Solve the Four Sum Problem?
Sort the array, then use two nested loops to fix the first two elements and a two-pointer sweep on the remainder to find pairs completing the target sum, givin…
How Do You Find a Subarray With a Given Sum?
For arrays of non-negative integers, a sliding window expands and shrinks two pointers over the array to find a contiguous subarray summing to a target in O(n)…