Skip to content
Preptima

Top 6 Two pointers Interview Questions

Browse 6 real-world technical and behavioral interview questions about Two pointers. Review scenarios, edge cases, and architectural best practices.

Detect whether a linked list has a cycle, and return the node where the cycle begins.

Advance one pointer by one node and another by two; if they ever coincide there is a cycle. Then reset one pointer to the head and step both one node at a time, and they meet at the cycle's entry. O(n) time, O(1) space, and the arithmetic behind the second phase is what the follow-up asks for.

mediumcoding

You need the longest palindrome in a string. Where does centre-expansion cost you, and what does it cost?

Every palindrome is fixed by its centre, and a string of length n has 2n minus 1 of them once the gaps between characters are counted. Expanding outwards from each is O(n squared) worst case in O(1) extra space, and the worst case is reached by a string of one repeated character, where the expansions from every centre run all the way to the boundary.

mediumcoding

Find the length of the longest substring without repeating characters.

Maintain a sliding window with a map from character to its last seen index; when a duplicate appears inside the current window, jump the left boundary past that previous occurrence rather than shrinking one step at a time, giving a single-pass O(n) solution. It also connects two pointers to the point an interviewer is testing.

mediumcoding

Remove the nth node from the end of a singly linked list in a single pass.

Run one pointer n nodes ahead of another, then advance both until the leader reaches the last node, leaving the follower on the predecessor of the node to unlink. A dummy node in front of the head is what removes the separate case for deleting the head itself.

mediumcoding

Return every distinct triplet in the array that sums to zero.

Sort the array, fix each element as an anchor, then converge two pointers inwards from both ends of the remaining range. The sort is asymptotically free against the O of n squared scan, and it is what lets you skip duplicate values positionally instead of deduplicating a set of results afterwards. It also connects sorting to the point an interviewer is testing.

mediumcoding

Given the heights of a row of bars, work out how much rainwater is trapped between them. Do it in constant extra space.

Water above a bar is the smaller of the tallest bar to its left and the tallest to its right, minus its own height. Walking inwards from both ends and always advancing the shorter side works because that side's running maximum is the binding constraint there, whatever heights remain unseen in the middle.

mediumcoding