This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 1: Arrays and strings
Two indices, each move ruling out a whole row of pairs.
Builds on Hash Maps & Sets.
The question to ask
If I compare the two ends, does the result tell me which end can't possibly be in the answer?
When the input is sorted (or you can sort it), comparing the values at two indices tells you which one can't be part of the answer, so you move it and never look back. Opposite ends walking inward handle pair-sum and palindrome questions; a slow write pointer behind a fast read pointer handles in-place filtering. Either way it's O(n) time and O(1) space, which is the usual follow-up to a hash-map answer.
Want to watch it run first? Step through the interactive walkthrough.
Time O(n), or O(n log n) if you sort first · Space O(1)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
The write pointer marks the end of the kept prefix; the read pointer scans everything. Anything kept is copied to write, then write advances. It's the shape behind Remove Duplicates, Move Zeroes and Remove Element.
function removeDuplicates(nums: number[]): number {
let write = 0;
for (let read = 0; read < nums.length; read++) {
if (read === 0 || nums[read] !== nums[write - 1]) {
nums[write] = nums[read];
write++;
}
}
return write;
}3Sum: sort, then for each anchor run the inward walk on the suffix. Skip equal anchors and equal neighbours after a hit, or the answer fills with duplicate triplets.
function threeSum(nums: number[]): number[][] {
const sorted = [...nums].sort((a, b) => a - b);
const result: number[][] = [];
for (let i = 0; i < sorted.length - 2; i++) {
if (i > 0 && sorted[i] === sorted[i - 1]) continue;
let left = i + 1;
let right = sorted.length - 1;
while (left < right) {
const sum = sorted[i] + sorted[left] + sorted[right];
if (sum < 0) left++;
else if (sum > 0) right--;
else {
result.push([sorted[i], sorted[left], sorted[right]]);
left++;
right--;
while (left < right && sorted[left] === sorted[left - 1]) left++;
}
}
}
return result;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
After lowercasing and dropping non-alphanumeric characters, does the string read the same forwards and backwards? Return whether it is a palindrome.
"Palindrome" means comparing mirrored positions: one pointer from each end, skipping characters that don't count.
Not attempted yet
Merge the second sorted array into the first, which has spare room at the end. Do it in-place and keep the result sorted.
"Merge" two sorted arrays "in-place": fill from the back with one pointer per array, so nothing gets overwritten before it's read.
Not attempted yet
Move all zeros to the end of the array in-place, keeping the relative order of the non-zero elements.
"Move all" plus "in-place": a write pointer collects non-zero values in order, then the tail is zero.
Not attempted yet
Given a sorted array, remove duplicates in-place so each value appears once, and return how many unique values remain.
Sorted means duplicates are adjacent. "Remove duplicates in-place" is the read and write pointer shape.
Not attempted yet
The array is sorted in non-decreasing order. Find the pair that sums to target and return their 1-based indices, using only constant extra space.
Same question as Two Sum, but "sorted" and "constant extra space" take the hash map away and hand you two pointers instead.
Not attempted yet
Return all unique triplets in the array that sum to zero. The answer must not contain duplicate triplets.
"Triplets" that "sum to zero": sort, fix the first element, two-pointer the rest, skip repeats.
Not attempted yet
Given wall heights, pick two walls that together with the x-axis form a container holding the most water. Return that area.
Start widest. The shorter wall caps the height, so moving the taller one can never help. Always move the shorter pointer.
Not attempted yet
Sort an array of 0s, 1s and 2s in-place in one pass, without the library sort.
Three values, one pass, "in-place": Dutch national flag with three pointers (low, mid, high).
Not attempted yet
Given bar heights, compute how much rain water is trapped between the bars after it rains.
Water above a bar is min(max left, max right) minus its height. Two pointers carry both maxima inward, moving whichever side is lower.
Not attempted yet