Meta interview question

Leetcode medium two questions. backtracking