Pinterest Interview Question

basic backtracking and counting problems.