Home › Company-wise questions › Deloitte › Topological Sort / Course Schedule (II) ← Deloitte bank
Deloitte · Question 57 · Graphs
Topological Sort / Course Schedule (II) Graphs Java code ⏱ O(V+E) 💾 O(V+E)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Produce an ordering of courses given prerequisites using Kahn's BFS topological sort.
Approach Emit zero-indegree courses; leftovers mean a cycle.
Time O(V+E)Space O(V+E)
How it works 1 Build the graph and indegrees. 2 Emit zero-indegree courses BFS-style. 3 Full order means feasible; else empty. Sample input / output Input:
numCourses = 2, prerequisites = [[1, 0]]
Output:
[0, 1] Constraints 1 <= numCourses <= 2000.
Solution — read it, then walk it Flowchart Pseudocode
Time O(V+E)Space O(V+E)Loops 5Branches 2Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE static int[] findOrder(int numCourses, int[][] pre) List<List<Integer>> g = new ArrayList<>() for (int i = 0; i < numCourses; i++) g.add(ne… DEFINE [] indeg = new int[numCourses] for (int[] p : pre) g.get(p[1]).add(p[0]) indeg[p[0]]++ Queue<Integer> q = new ArrayDeque<>() for (int i = 0; i < numCourses; i++) if (indeg[i] == 0) q.add(i) DEFINE [] res = new int[numCourses] DEFINE idx = 0 while (!q.isEmpty()) DEFINE u = q.poll() res[idx++] = u for (int v : g.get(u)) if (--indeg[v] == 0) q.add(v) RETURN idx == numCourses ? res : new int[0] DEFINE static void main(String[] args) System.out.println(Arrays.toString(findOrder(2, new int[][]{{1, 0}}))) END start / end statement condition loop returns definition← Q56 · Top K Frequent Elements Q58 · Merge k Sorted Lists → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →