CG Infinity interview question

Basic java logic