Moon Active interview question

How I implemented the solution