Spotify Interview Question

Python and space time complexity