Why Two Pointer Technique Beats Brute Force in DSA Typescript - Performance Analysis
We want to see why using two pointers is faster than checking every pair one by one.
How does the number of steps change when input grows?
Analyze the time complexity of the following code snippet.
function twoPointerSum(arr: number[], target: number): boolean {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) return true;
else if (sum < target) left++;
else right--;
}
return false;
}
This code checks if two numbers in a sorted array add up to a target using two pointers moving inward.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop moves pointers inward checking sums.
- How many times: At most, each pointer moves across the array once, so up to n steps total.
As the array gets bigger, the number of steps grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: Doubling input roughly doubles the steps needed.
Time Complexity: O(n)
This means the steps grow directly with the size of the input array.
[X] Wrong: "Two pointers still check every pair, so itβs as slow as brute force."
[OK] Correct: Two pointers smartly skip many pairs by moving inward, so they do fewer checks than brute force.
Knowing why two pointers are faster shows you can choose better ways to solve problems, a skill interviewers value.
"What if the array was not sorted? How would the time complexity change if we still tried two pointers?"
Practice
Solution
Step 1: Understand brute force approach
Brute force checks all pairs, leading to many unnecessary comparisons.Step 2: Analyze two pointer technique advantage
Two pointers move inward based on conditions, skipping unnecessary checks and reducing time.Final Answer:
It reduces the number of comparisons by moving pointers inward based on conditions. -> Option AQuick Check:
Two pointer skips pairs, faster than brute force [OK]
- Thinking recursion is always faster
- Confusing sorting with two pointer technique
- Assuming extra memory is used in two pointer
arr?Solution
Step 1: Identify valid pointer indices
Array indices start at 0 and end at length - 1.Step 2: Check each option
let left = 0, right = arr.length - 1; correctly sets left to 0 and right to last index (length - 1). Others use invalid indices.Final Answer:
let left = 0, right = arr.length - 1; -> Option BQuick Check:
Array indices start at 0 [OK]
- Starting pointer at 1 instead of 0
- Using arr.length as index (out of bounds)
- Swapping left and right pointers
const arr = [1, 2, 3, 7, 8, 9];
let left = 0;
let right = arr.length - 1;
let found = false;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === 10) {
found = true;
break;
} else if (sum < 10) {
left++;
} else {
right--;
}
}
console.log(found);What will be printed?Solution
Step 1: Trace pointer values and sums
Start: left=0(arr[0]=1), right=5(arr[5]=9), sum=10 -> found=true, loop breaks.Step 2: Confirm output
Since found is set to true, console.log prints true.Final Answer:
true -> Option DQuick Check:
1 + 9 = 10 found [OK]
- Not breaking loop after finding sum
- Confusing pointer increments
- Assuming array is unsorted
const arr = [2, 4, 6, 8, 10];
let left = 0;
let right = arr.length;
let found = false;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === 15) {
found = true;
break;
} else if (sum < 15) {
left++;
} else {
right--;
}
}
console.log(found);What is the bug?Solution
Step 1: Check pointer initialization
right is set to arr.length which is out of bounds (max index is length - 1).Step 2: Understand impact of bug
Accessing arr[right] causes runtime error or undefined behavior.Final Answer:
right pointer is initialized out of array bounds -> Option AQuick Check:
Array indices max is length-1 [OK]
- Starting left pointer at 1
- Changing sum target incorrectly
- Using <= instead of < in loop
arr = [1, 3, 4, 5, 7, 10, 12]. Using the two pointer technique, how many unique pairs sum to 13?Solution
Step 1: Initialize pointers and check sums
left=0(arr[0]=1), right=6(arr[6]=12), sum=13 -> count=1, move left++ and right--Step 2: Continue checking pairs
left=1(arr[1]=3), right=5(arr[5]=10), sum=13 -> count=2, move left++ and right--Step 3: Check next pair
left=2(arr[2]=4), right=4(arr[4]=7), sum=11 < 13 -> move left++Step 4: Check final pair
left=3(arr[3]=5), right=4(arr[4]=7), sum=12 < 13 -> move left++Step 5: Pointers crossed, stop
Total pairs found = 2Final Answer:
2 -> Option CQuick Check:
Pairs (1,12) and (3,10) sum to 13 [OK]
- Counting pairs with same elements twice
- Not moving pointers correctly after sum check
- Assuming pairs must be adjacent
Ready to prove it?
Pass the test and this pattern moves unknown
