Square interview question

Given the provided problem, write out a way to solve it using code.