Pinterest interview question

basic backtracking and counting problems.