AT&T interview question

Star method was used for complete solutions