The Concept

The two-pointer pattern refers to the idea of using two-pointers to efficiently traverse a linear data structure (e.g. arrays) to solve a given problem. The two-pointers typically start at different points (e.g. the beginning and end of a list) and move across the data structure in opposite directions, given that a specific condition is met.

Problems that the pattern solves

A coding problem can be solved using this pattern if it:

Pre-requisites to using the pattern

To use this pattern, the following checks need to be met:

The Code

We can leverage this pattern, for example, to check whether a word is a palindrome. We're techies, not literature majors, so let me educate you on what a palindrome is 😄. A palindrome is a word that is spelled the same forward and backward e.g. Abba, Eve, Anna.

The Problem

Given a string word, return true if it’s a palindrome, and false if it is not.

Explanation

Screenshot 2024-11-22 at 11.43.46.png