Find a valid dependency order | Amazon Interview Question