Iscar Interview Question

Sql question and one backtracking