ByteDance Interview Question

Graph problem, likely to be topological sort