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

Starter: last string index

Why does the last index use length minus one? Goal: Read the last character without using the first invalid index.

Starter step 12 of 20Work with strings

String indexes stop at length minus one.

The first invalid index is exactly the string length.

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. Why does the last index use length minus one?

  2. The last character is at length minus one. The right choice is "Indexes start at 0, so the final valid index is one less than length" because it preserves the goal: Read the last character without using the first invalid index.

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

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
1// Goal: Read the last character without using the first invalid index.
2function starterExample() {
3 const word = "code";
4 const last = word[word.length - 1];
5}

Why does the last index use length minus one?