Detect cycle in a directed graph | Amazon Interview Question