Big O of JavaScript arrays

NOTE: While this answer was correct in 2012, engines use very different internal representations for both objects and arrays today. This answer may or may not be true. In contrast to most languages, which implement arrays with, well, arrays, in Javascript Arrays are objects, and values are stored in a hashtable, just like regular object … Read more

Maximum single-sell profit

I love this problem. It’s a classic interview question and depending on how you think about it, you’ll end up getting better and better solutions. It’s certainly possible to do this in better than O(n2) time, and I’ve listed three different ways that you can think about the problem here. Hopefully this answers your question! … Read more

Quick sort Worst case

Quicksort’s performance is dependent on your pivot selection algorithm. The most naive pivot selection algorithm is to just choose the first element as your pivot. It’s easy to see that this results in worst case behavior if your data is already sorted (the first element will always be the min). There are two common algorithms … Read more

What is pseudopolynomial time? How does it differ from polynomial time?

To understand the difference between polynomial time and pseudopolynomial time, we need to start off by formalizing what “polynomial time” means. The common intuition for polynomial time is “time O(nk) for some k.” For example, selection sort runs in time O(n2), which is polynomial time, while brute-force solving TSP takes time O(n · n!), which … Read more