Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Algorithms
Algorithmseasy

What is the two pointers technique?

Tags
#two-pointers#array#technique
Back to categoryPractice quiz

Answer

You keep two indices (e.g., left/right) and move them based on a rule, often on a sorted array or a sliding window. It can reduce nested loops to O(n) in many problems.

function hasPairSum(arr: number[], target: number) {
  let l = 0;
  let r = arr.length - 1;

  while (l < r) {
    const sum = arr[l] + arr[r];
    if (sum === target) return true;
    if (sum < target) l++;
    else r--;
  }

  return false;
}

Related questions

Algorithms
Boyer–Moore majority vote: what does it solve and what’s the core idea?
#majority-vote#array#linear-time
Algorithms
Floyd’s cycle detection (tortoise and hare): what does it detect and what are its time/space costs?
#cycle-detection#tortoise-hare#linked-list
Algorithms
Sliding window: what is it and when is it better than nested loops?
#sliding-window#two-pointers#complexity
Algorithms
What does Kadane’s algorithm solve?
#kadane#dynamic-programming#array