I recently gave amazon 2023 new grad SDE OA, and face this question, I would really appreciate if someone can help to solve this in JavaScript.