Intermediate·Concept·2 min read

Sliding window and two pointers in JavaScript

Practice this question

Sliding window

Use when you need a contiguous subarray or substring.

Problems to practice

  • Longest substring without repeating characters
  • Maximum sum subarray of size K
  • Minimum window substring
  • Permutation in a string

Two pointers

Used for sorted arrays and palindrome checks.

Example: valid palindrome

JavaScript
function isPalindrome(s) {
  let left = 0;
  let right = s.length - 1;

  while (left < right) {
    if (s[left] !== s[right]) return false;
    left++;
    right--;
  }

  return true;
}

Adapted from 16_DSA_JAVASCRIPT.md · “12. Sliding window and two pointers in JavaScript”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.