How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
public class CourseSchedule { //Directed Graph //Topological Sort Using Kahn's Algorithm (BFS) public boolean canFinish(int numCourses, int[][] prerequisites) { int[] inDegree = new int[numCourses]; List<List<Integer>> adj = new ArrayList<>(numCourses); for(int i = 0; i < numCourses; i++) { adj.add(new ArrayList<>()); } for(int[] prerequisite : prerequisites) { //To take course a, you must first take course b. int dest = prerequisite[0]; // course to be taken later int src = prerequisite[1]; // course that must be taken first adj.get(src).add(dest); // Create the adjacency list; as in the graph representation. Directed edge from src to dest; Directed Graph inDegree[dest]++; } Queue<Integer> queue = new ArrayDeque<>(); for(int i = 0; i < numCourses; i++) { if(inDegree[i] == 0) { queue.offer(i); } } int visitedNode = 0; while(!queue.isEmpty()) { int node = queue.poll(); visitedNode++; for(int neighbor : adj.get(node)) { inDegree[neighbor]--; // Delete the edge "node -> neighbor". So, backtracking?? if(inDegree[neighbor] == 0) { queue.offer(neighbor); } } } return visitedNode == numCourses; } //Time complexity: O(V + E) → V = number of courses, E = number of prerequisites //Space complexity: O(V + E) → adjacency list and in-degree array
//DFS with Cycle Detection
public boolean canFinishDFS(int numCourses, int[][] prerequisites) {
List<List<Integer>> adj = new ArrayList<>(numCourses);
for(int i = 0; i < numCourses; i++) {
adj.add(new ArrayList<>());
}
for(int[] prerequisite : prerequisites) {
int dest = prerequisite[0];
int src = prerequisite[1];
adj.get(src).add(dest);
}
boolean[] visited = new boolean[numCourses];
boolean[] recStack = new boolean[numCourses];
for(int i = 0; i < numCourses; i++) {
if(dfsCycleDetect(i, adj, visited, recStack)) {
return false;
}
}
return true;
}
private boolean dfsCycleDetect(int node, List<List<Integer>> adj, boolean[] visited, boolean[] recStack) {
if(recStack[node]) { // If the node is already in the stack, we have a cycle.
return true;
}
if(visited[node]) { // If already visited and not in recStack, no cycle from this node.
return false;
}
visited[node] = true; // Mark the current node as visited and part of current recursion stack.
recStack[node] = true;
for(int neighbor : adj.get(node)) {
if(dfsCycleDetect(neighbor, adj, visited, recStack)) {
return true;
}
}
recStack[node] = false; // Remove the node from recursion stack before backtracking.
return false;
}
//Time complexity: O(V + E) → V = number of courses, E = number of prerequisites
//Space complexity: O(V + E) → adjacency list, visited array, and recursion stack
}
— Written by Saurabh Patil • B.Tech CSE • Software Developer🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
🔒 Locked steps cannot be moved. Only unlocked steps are draggable.
🔊 Enable sound for swap feedback and completion effects.