Find a valid dependency order | Amazon Coding Question