Toptal interview question

some recursion backtracking algorithm