DEV Community

loading...

Discussion on: Basic JavaScript Interview Questions

Collapse
recursivefaults profile image
Ryan Latta

One alternative to the stack example is to use shift/unshift.

A benefit is it covers adding and removing items, and allows you to treat the index of 0 as the top without worrying about it.

Also, not many people pay attention to those methods so you might even come across as clever for having non-obivous knowledge of JS Arrays.

Collapse
jperasmus profile image
JP Erasmus

Using shift/unshift for the stack would not be a very efficient solution when using JS arrays under the hood because that would turn an O(1) action into O(n) since every item in the array would have to update its index every time to perform either of these actions. Pushing onto the end or popping from it is O(1) since only a single item is affected.

Collapse
recursivefaults profile image
Ryan Latta

Being able to talk about that makes a better interview.