One Pager Cheat Sheet
- The
Two Pointer Techniqueis an essential tool for software developers to use, especially when faced with technical interviews. - Pointers are
referencesto objects, and the two pointers technique is used to track and compare array or string indices to save time and space. - The
two-pointer techniqueis an efficient approach to processing two elements of adata structure, such as an array or list, per loop in order to solve problems involving collections. - The two-pointer technique is a
search algorithmused to solve problems involving collections such as arrays and lists by comparing elements pointed by two pointers and updating them accordingly. - By initializing two variables
pointer_oneandpointer_twoand evaluating their values with respect to the giventarget, we can find pairs in an array that sum up to a certain number with aO(n)time complexity. Two pointers are used in different programming languages to start from the ends of an array and iteratively narrow in to find thetarget` more efficiently than other techniques.- We can use two pointers starting from the beginning and end of an already sorted array to check if they 'sum up' to the target, which is done by a simple comparison statement
if sum == target. - The logic applied is to
increment the left pointerif the sum of values isless than the target valueanddecrement the right pointerif it ishigher than the target value. - Understand that if
arr[pointer_one]<target-arr[pointer_two],pointer_oneshould be moved forward to get closer to the desired magnitude. - The process of using a fast pointer and a slow pointer is another way to apply the two pointer technique, which can have
O(n)time complexity andO(1)space complexity. - Using
Slow and Fast Pointerscan detect cycles in alinked list, such as when a node points back to a previous node. - By setting
fastto traverse twice as quickly asslow, the distance between them increases at each step. - If both
pointersreach the same node, then there is a cycle present in thelinked list. - The
codeattached provides anO(n)or linear time complexity. - Yes, the two pointer technique can reduce both time and space complexity down to
O(n), thus improving overall performance. - Two-pointers are
usefulfor iterating over and combining two sorted arrays. - The two-pointer technique involves placing two pointers at opposite ends of an
arrayandcomparingtheir values toreverseits order by swapping or shifting items.


