Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
[Spoiler] inserting elements into temp list

John O'Neill

Sep 13, 2023

In the solution, each element is inserted into the temp_list array with temp_list.insert(0, arr[i]). According to this Python wiki, List insert has O(n) time complexity, so I used a deque, for which appendleft has O(1) time complexity, instead. Should the solution be changed to use a deque as well? The empty solution imports deque, so it seems to me that it might have been the intention to use it.

2

0

Comments
Comments
Design Gurus
Design Gurus2 years ago

Changing the solution to use deques MIGHT make it faster, but overall time complexity won't change. If you see following lines from python code:

                temp_list.insert(0, arr[i])                 # Add the current subarray to the result.        ...
J
John O'Neill2 years ago

Oops — forgot to include the wiki link. https://wiki.python.org/moin/TimeComplexity