Cresta (CA) interview question

ask me to explain the question itself, time complexity, improve my approach