Analyze time and space complexity | LinkedIn Interview Question