All puzzles › Two Pointers

Two Pointers Practice Problems

36 puzzles (12 easy, 14 medium, 10 hard), 7 free, in Python, JavaScript and C++.

Two pointers means walking a sequence with two positions instead of one, and moving them by a rule so that you never have to go back. One starts at each end and they close in, or both start at the front and one runs ahead. Done right it replaces a pair of nested loops with a single pass and uses no extra memory at all.

How to spot a two pointers problem

Look for sorted input, or a question about pairs, or anything that reads the same from both ends. The test is whether moving a pointer can safely rule out everything behind it. If the order of the data guarantees that, two pointers will work; if it does not, sort first or use a hash map.

New to it? Read the lesson first: Two Pointers.

The puzzles, easiest first

  1. Keep Only What Lined Up (Easy, free): In BB84, Alice sends each bit of a key as a photon prepared in one of two bases, written + and x, chosen at random.
  2. Squeeze the Stutter Out (Easy, free): A speech model outputs one symbol for every sliver of audio, so a single letter comes out many times in a row, with a special blank symbol in the gaps.
  3. Valid Palindrome (Easy, free): Given a string, return true if it reads the same forwards and backwards, ignoring case and anything that isn't a letter or number.
  4. Back to Front (Easy): Games draw far things first so near things paint over them, which is called the painter's algorithm.
  5. Cut the Silence (Easy): A recorded sound effect has silence at both ends, which delays it when it plays.
  6. Evens to the Front (Easy): Given an array, rearrange it so every even number comes before every odd one.
  7. Is Subsequence (Easy): Given strings s and t, return true if you can find the letters of s inside t, in order, possibly with other letters in between.
  8. Move Zeroes (Easy): Given an array, move every 0 to the end and keep the other values in their original order.
  9. Reverse It In Place (Easy): Given a string, reverse it by swapping characters from both ends inward.
  10. Squeeze Out Repeats (Easy): Given a sorted array, remove the duplicates and return what's left.
  11. Strip Out a Value (Easy): Given an array and a value, remove every copy of that value and return what's left, in order.
  12. Weave Two Words (Easy): Given two words, weave them together one letter at a time, starting with the first word.
  13. Copy From What You Already Wrote (Medium, free): LZ77, the idea inside nearly every compressor, replaces repeated data with a pointer backward: go back this far and copy this many.
  14. Crowded Enough to Count (Medium, free): DBSCAN finds clusters of any shape by looking for crowded places.
  15. Who Happened First (Medium, free): Machines in a distributed system cannot trust each other's wall clocks, so each event carries a vector clock: one counter per node.
  16. A Gap of Exactly d (Medium): Given a sorted array and a number d, return true if two different items are exactly d apart.
  17. Container With Most Water (Medium): Given a list of wall heights, pick two walls that hold the most water between them and return that amount.
  18. Merge Backwards (Medium): Given a sorted array a with empty space at the end, the number of real values m, and a second sorted array b, merge b into a and return it.
  19. Negatives to the Left (Medium): Given an array, move every negative number in front of every other number.
  20. Pairs Under the Limit (Medium): Given an array and a limit, return how many pairs add up to less than the limit.
  21. Reverse Only the Vowels (Medium): Given a string, reverse only its vowels and leave every other character where it is.
  22. Shuffle Without Bias (Medium): Given a list and a list of draws, shuffle the list and return it.
  23. Squares, Still Sorted (Medium): Given a sorted array that may have negatives, return the squares of every number, sorted.
  24. Two Sum (Sorted) (Medium): Given a sorted array and a target, return the indices of the two numbers that add up to the target.
  25. What They Share (Medium): Given two sorted arrays, return the values that are in both, with no repeats.
  26. Where Are They Now? (Medium): Given snapshot times and positions, the current time, and a lag, return where to draw the player, rounded to three decimal places.
  27. Newest Wins, Deletions Vanish (Hard, free): A log-structured store never edits data in place.
  28. As Close As You Can (Hard): Given an array and a target, return the sum of the three numbers that comes closest to the target.
  29. Count Every Mirror (Hard): Given a string, return how many of its substrings are palindromes, counting every single letter.
  30. Longest Mirror Inside (Hard): Given a string, return its longest palindrome substring.
  31. Nearest to Nothing (Hard): Given an array, return the sum of the pair that comes closest to 0.
  32. One Deletion Allowed (Hard): Given a string, return true if deleting at most one character makes it a palindrome.
  33. The Part That Is Wrong (Hard): Given an array, return the length of the shortest stretch that, if you sorted just that stretch, would make the whole array sorted.
  34. Three Buckets, One Pass (Hard): Given an array of 0s, 1s, and 2s, sort it in ONE pass.
  35. Three That Cancel (Hard): Given an array, return every unique set of three numbers that adds up to 0.
  36. Trapping Rain Water (Hard): Given a list of bar heights, return how much rainwater gets trapped between them.

Practice another pattern

Or browse by subject

Browse all 676 puzzles