Amazon Interview Question

it was a leetcode question