Decoding the Two Pointer Technique in a Coding Interview Using Javascript

2024 ж. 13 Мам.
7 080 Рет қаралды

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.

    @fancyAlex1993@fancyAlex1993 Жыл бұрын
  • Thank you for a really thorough explanation of the algorithm, as well as the brute force option. I found it really helpful.

    @Sionnachsalach@Sionnachsalach2 ай бұрын
  • You are the best, Liz! Thank you!!!

    @samuelboatengtakyi7070@samuelboatengtakyi7070Ай бұрын
  • Very Well Explained i like your teaching way and Content very Helpful Thank you so much

    @ahmedbaig8543@ahmedbaig85434 ай бұрын
  • And Oscar for great two pointer videos goes to 👏👏👏

    @jamalmahamoud8659@jamalmahamoud86592 жыл бұрын
  • Congrats on moving out.... Your an absolute gem of a teacher

    @nunyabizness1269@nunyabizness12692 жыл бұрын
  • Great video on two pointers, thank u so much for this 😍

    @arunkaiser@arunkaiser2 жыл бұрын
  • Congratulations 👏👏 for the new flat. Your videos are really great 👍

    @yassine20909@yassine209092 жыл бұрын
  • Thank you so much!

    @iiimiiim@iiimiiim Жыл бұрын
  • 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)

    @vino8711@vino87112 жыл бұрын
    • This helped debug one of my test cases. Thank you!

      @nathanf300@nathanf300 Жыл бұрын
    • 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.

      @amorfati4559@amorfati4559 Жыл бұрын
    • @@nathanf300 could you please share the test case? Thanks!

      @samuelboatengtakyi7070@samuelboatengtakyi7070Ай бұрын
  • Great and simple at the same time.

    @minnuss@minnuss2 жыл бұрын
    • 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)

      @minnuss@minnuss2 жыл бұрын
  • Thanks

    @TheSamfat@TheSamfat2 жыл бұрын
  • thanks!

    @tomerdun@tomerdun Жыл бұрын
  • clearer than water

    @ivozayas678@ivozayas67811 ай бұрын
  • 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.

    @davitti7036@davitti70362 жыл бұрын
KZhead