Spotify interview question

Python and space time complexity