EY interview question

Asked to code for the given scenario.