Decoding the Two Pointer Technique in a Coding Interview Using Javascript
We’re continuing our common coding interview patterns series by introducing the two pointer pattern. This patten is helpful in problems where you want to avoid looping over an array or linked list multiple times.
The Challenge:
Given an array of sorted numbers and a target sum, find a pair in the array whose sum is equal to the given target.
Video Overview:
0:44 Two pointer pattern overview
2:54 Problem
3:39 Naive approach
5:46 Two pointer approach
7:43 Javascript solution
16:32 Test cases
17:33 Recap
Please subscribe to our channel!
What types of videos would you like to see next? Let us know in the comments.
Additional Resources:
* Practice hundreds of real coding challenges at coderbyte.com/
* Need more practice? Check out our channel for more videos on preparing for a coding interview / coderbytedevelopers
Congratulations. You are an absolute beast of a teacher.
Thank you for a really thorough explanation of the algorithm, as well as the brute force option. I found it really helpful.
You are the best, Liz! Thank you!!!
Very Well Explained i like your teaching way and Content very Helpful Thank you so much
And Oscar for great two pointer videos goes to 👏👏👏
Congrats on moving out.... Your an absolute gem of a teacher
Great video on two pointers, thank u so much for this 😍
Congratulations 👏👏 for the new flat. Your videos are really great 👍
Thank you so much!
Suggestion for an adjustment Given the example you use i.e [1,2,3,4,5] target 7 and applying the two pointer base on these as you stated earlier -> pairSum === targetSum -> return it -> pairSum < targetSum -> leftPointer ^ -> pairSum > targetSum -> rightPointer~^ * first it is a cool approach because it is a one-way approach or O(N) but with some limitations. * assuming that the target is 6, which requires combination of [2,4], and the 4 is going to come from the rightPointer~^ as expected. * Thus, before the [rightPointer~^] will occurred the while-loop must have end at combination [5,5] because of the condition of the while-loop-otherwise (leftPointer == rightPointer) I suggest the while-loop (pair != targetSum)
This helped debug one of my test cases. Thank you!
I'm not totally getting your point. If the target is 6, wouldn't that be caught with the [1, 5] pair already, not [2, 4]? Then you don't have to decrement the right pointer. I think I see what you're getting at, but I can't think of an example where it is applicable.
@@nathanf300 could you please share the test case? Thanks!
Great and simple at the same time.
I would like to add one more line of code before the loop starts, and that is a need for sorting of the array. arr = arr.sort((a,b) => a-b)
Thanks
thanks!
clearer than water
What type of corny video is this? Didn't even run the algorithm. Your solution can't even solve trivial cases like this findPair([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30], 22). Goes on a never ending search for an obvious answer. Explains the views. You should still be learning not teaching.