bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
āœļøFill the BlankjavascriptEasyTwo Pointers

Starter: keep looping safely

Fill the boundary that keeps the index inside the list. Goal: Walk through every valid index without reading past the end.

Starter step 5 of 20Read code one line at a time

Use the loop boundary and final return together.

The two pointers move inward; the blank should keep them inside the string.

Problem Brief

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.

Puzzle Hints
  1. Read the blank inside this exact statement: "while (index ___ nums.length) {".

  2. Array indexes stop before length, so the loop uses <. The correct token works because it preserves the goal: Walk through every valid index without reading past the end.

  3. Check the variable that is read immediately after the blank; that usually tells you what the blank must produce.

Asked at 17 companies
AdobeAmazonAmerican Express
Valid Palindrome — Two Pointers
two pointers
leftrightr0a1c2e3c4a5r6
left=0
right=6
1
5

Input: "racecar". Compare from both ends.

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

Drag tokens into the blanks

<
null
1// Goal: Walk through every valid index without reading past the end.
2function starterExample() {
3 let index = 0;
4 while (index ___ nums.length) {
5 index += 1;
6 }
7}