Cruise interview question

Medium level questions from leetcode