Two Pointers from Both Ends
Problem¶
When you see "two numbers/characters/nodes from opposite ends", "palindrome", "reverse", "sorted arrays/lists", or "compare from both sides"
Strategy: Two Pointers from Both Ends¶
- Think about this
- Think two pointers from left and right.
- On strings, ask whether you can skip invalid characters on the fly instead of preprocessing.
- On sorted structures, ask whether pointer movement can discard impossible regions.
- Avoid this
- Do not build cleaned copies unless space is irrelevant.
- Do not forget whether equality/order is by value or by index.
- Do not miss odd-length center handling.
- Tell this to the interviewer
- Explain why each pointer move is safe:
- mismatch proves failure
- match lets both move inward
- sorted order tells you which side to advance
- Give the stopping condition clearly.
- Explain why each pointer move is safe:
- See these problems