Amazon interview question

Solve a backtracking problem and TC.