Amazon Interview Question

It was a standard leetcode coding question.