Algorithm · Apple · Medium
You are handed a directed graph and must produce an ordering of its vertices in which every edge points forward. If the graph has a directed cycle, no such ordering exists and the answer is an empty array. The graph contains n vertices labeled 0 to n-1. An edge [u,v] says that vertex u has to come earlier than vertex v in the ordering. Many valid orderings can exist, so the answer must be the lexicographically smallest one. This requirement is an addition made so that…