BNY interview question

Make sure to know time and space complexity