Why is builtin sorted() slower for a list containing descending numbers if each number appears twice consecutively?

As alluded to in the comments by btilly and Amadan, this is due to how the Timsort sorting algorithm works. Detailed description of the algorithm is here. Timsort speeds up operation on partially sorted arrays by identifying runs of sorted elements. A run is either “ascending”, which means non-decreasing: a0 <= a1 <= a2 <= … Read more

Can a Fibonacci function be written to execute in O(1) time?

Here is a near O(1) solution for a Fibonacci sequence term. Admittedly, O(log n) depending on the system Math.pow() implementation, but it is Fibonacci w/o a visible loop, if your interviewer is looking for that. The ceil() was due to rounding precision on larger values returning .9 repeating. Example in JS: function fib (n) { … Read more