Amazon interview question

SWE leetcode question around easy-medium difficultly.