Preamble
Two pointers on sorted arrays show up constantly: pair sums, removing duplicates in-place, merging sequences. The algorithm is tiny; the invariants matter. This post keeps Python and Java side by side so syntax does not obscure the shared logic.
Invariants to narrate
Pointers i and j partition the remaining search space. When a[i] + a[j] is too small, only increasing i can raise the sum; when too large, only decreasing j can lower it. Saying that aloud in interviews beats mumbling loops.
Python sketch
def pair_sum_sorted(a, target):
i, j = 0, len(a) - 1
while i < j:
s = a[i] + a[j]
if s == target:
return i, j
if s < target:
i += 1
else:
j -= 1
return None
Java sketch
Same indices with int[], explicit lengths, and conventional while (i < j) loops. Prefer for only when it clarifies bounds; two-pointer problems are usually clearer with while.
Conclusion
Language syntax is secondary; invariants are primary. Hash Maps Under the Hood: dict and HashMap Intuition discusses hash maps when keys and equality cross Python/Java boundaries.