Amazon interview question

Two questions at Leetcode medium difficulty