bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
šŸŽÆCode CheckjavascriptMediumTwo Pointers

Starter: choose a tiny test

Which input checks the one-item edge case? Goal: Use the smallest non-empty array so the length check proves the edge case.

Starter step 10 of 20Move through arrays

Choose an input that proves pointer movement works.

Prefer the test that stresses the smallest or tightest pointer window.

Problem Brief

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container.

Puzzle Hints
  1. Which input checks the one-item edge case?

  2. A one-item array is a small boundary test. The right choice is "[7]" because it preserves the goal: Use the smallest non-empty array so the length check proves the edge case.

  3. Tie your answer back to Two Pointers: what data is stored, when it updates, and what condition uses it?

Asked at 15 companies
AdobeAmazonApple
Container With Most Water — Two Pointers
two pointers
leftright108162235445863778
left=0
right=8
1
6

Start with widest container. Area = min(1,7) Ɨ 8 = 8

Two Pointers in JavaScriptref

Two pointers reduce O(n²) to O(n) on sorted arrays. Use left/right indices moving inward, or slow/fast for linked list patterns. Array methods like sort() prepare data for pointer techniques.

-let l = 0, r = arr.length - 1
-Move pointers based on sum vs target
-while (l < r) is the standard loop
-Works on sorted data — sort first if needed
function twoSum(nums, target) {
  let l = 0, r = nums.length - 1;
  while (l < r) {
    const sum = nums[l] + nums[r];
    if (sum === target) return [l, r];
    sum < target ? l++ : r--;
  }
}
Official docs →
Two Pointers in JavaScriptref

Two pointers reduce O(n²) to O(n) on sorted arrays. Use left/right indices moving inward, or slow/fast for linked list patterns. Array methods like sort() prepare data for pointer techniques.

-let l = 0, r = arr.length - 1
-Move pointers based on sum vs target
-while (l < r) is the standard loop
-Works on sorted data — sort first if needed
function twoSum(nums, target) {
  let l = 0, r = nums.length - 1;
  while (l < r) {
    const sum = nums[l] + nums[r];
    if (sum === target) return [l, r];
    sum < target ? l++ : r--;
  }
}
Official docs →
How to think: Two Pointersguide

The input is sorted (or can be sorted), and you need to find pairs or partitions.

1.Ask: "Is the input sorted or should I sort it?" → Two pointers might work
2.Ask: "Am I looking for a pair that sums to X?" → Left + Right pointers
3.Ask: "Am I checking symmetry (palindrome)?" → Left + Right moving inward
4.Ask: "Am I partitioning or removing in-place?" → Slow + Fast pointers
5.Key insight: sorted data lets you decide which pointer to move based on the sum/comparison

vs Hash Map: Data is already sorted — two pointers uses O(1) space vs O(n)

vs Brute force nested loops: Sorted data means you can eliminate half the pairs each step

sorted arraytwo numberspalindromepairin-placeremove duplicates
How to think: Two Pointersguide

The input is sorted (or can be sorted), and you need to find pairs or partitions.

1.Ask: "Is the input sorted or should I sort it?" → Two pointers might work
2.Ask: "Am I looking for a pair that sums to X?" → Left + Right pointers
3.Ask: "Am I checking symmetry (palindrome)?" → Left + Right moving inward
4.Ask: "Am I partitioning or removing in-place?" → Slow + Fast pointers
5.Key insight: sorted data lets you decide which pointer to move based on the sum/comparison

vs Hash Map: Data is already sorted — two pointers uses O(1) space vs O(n)

vs Brute force nested loops: Sorted data means you can eliminate half the pairs each step

sorted arraytwo numberspalindromepairin-placeremove duplicates
1// Goal: Use the smallest non-empty array so the length check proves the edge case.
2function starterExample() {
3 const nums = [7];
4 return nums.length === 1;
5}

Which input checks the one-item edge case?