Master the Two Pointer Trick in JavaScript: Solve Problems Faster!

preview_player
Показать описание
Walk through of one of the most useful techniques, the two pointer, definitely worth knowing for coding interviews at companies like Microsoft, Amazon, Apple and Google

#softwareengineering #javascript #codinginterview #shorts
Рекомендации по теме
Комментарии
Автор

On much bigger arrays, an integration using gradients would be generally much faster, jumping to the solution with much bigger steps than from one item to the next, avoiding many comparisons that are very unlikely to be the last one.
There are various way to implement that. Feel free to ask me.

emjizone
Автор

i love tips like this, very interesting and gives a wider perspective

infeza
Автор

Damn I finna thought you were gonna use pointers in javascript.

wizardnotknown
Автор

Nitpicky term clarification: you have two indices, not pointers

matthiskalble
Автор

well i think that you could do it easier way, i would say like while(left < right) { if ( arr[ left ] === ! arr[ right ] ) { console.log... ; break; } } i hope you understand this, i think it is little bit easier way of doing it

djukicar
Автор

There are even better and more performant ways of doing this in JavaScript

GeraldBosley-cfng
Автор

I popped the first/last element recursively and returned when the ternary expression is 0..

geldoku
Автор

For this technique is it necessary that the array should be sorted?

aniruddhadas
Автор

I've only seen it used on this problem. Is there any other practical application for this?

cabbage-dev
Автор

Without offense. Subtitles would be great for these. Accent could be difficult to understand. Personally I had to rewind many times to understand you wanted the summ to be equal to 0, making it harder for me to follow along. Thanks for the content!

cspjdbh
Автор

Obviously, this has interest *only if you get the sorted array from some mysterious black box you cannot hack or replace by you own sorting algorithm.* Otherwise, an extremely more overall efficient way would be to detect and index the pairs of items during the sort, since you have to compare them anyways, and more generally to update the index of pair of items during any edition (insert or remove) of the array.
This two-point or "bracket" approach is extremely more interesting when applied to multi-dimensional problems solved by dynamic programming, like in search substring algorithm for example.

emjizone
Автор

Can someone explain to me why the time complexity is O(1) not O(n)?

ZeyadMohamed-xsew
Автор

What his video does not tell you, is that assuming there is a relation between the difference between two values and the number of other other possible values between them (for example if you know they can be integers only and can't be repeated), you can make this algorithm even much faster, by using the *probability* to find a given value in a given interval of indexes.

If you can't assume this relation, aka if the values are completely random in a continuous space before sort, or if there are no limitation of the number of repetition of the same value, you're skrewed… I mean you can't do better than the algorithm in this video. Which is already good.

The nice thing with the digital machines is that they *cannot* work in continuous space. They work on discrete values by nature. Even "floats" are integers in disguise. So, yeah… feel free to ask me for even faster algorithms on huge tables.

emjizone
Автор

They are just indecies, not technically pointers.

DavidNewmon
Автор

Will two pointers technique work with unsorted array?

carefree_ladka
Автор

It will fail if both 3 -3 exist in left array or right array

madhuk
Автор

Isn't this tortous vs hare conjecture

weacro
Автор

When you turn two simple for loops into this mess...

igorfujs
Автор

it's... slow
if you want the other value, just make it hashcode-based so you just do a .get() with the negative version of the number you want

jujuteuxOfficial
welcome to shbcf.ru