EY Interview Question

Asked to code for the given scenario.