# 20 Topological Sort Interview Questions and Answers

Prepare for the types of questions you are likely to be asked when interviewing for a position where Topological Sort will be used.

Prepare for the types of questions you are likely to be asked when interviewing for a position where Topological Sort will be used.

In computer science, a topological sort is an ordering of vertices in a directed acyclic graph (DAG). That is, it is a linear ordering of vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. A topological sort is sometimes also called a topological ordering.

Topological sorting has applications in scheduling and data dependencies (for example, in a makefile). It also forms the basis for many graph algorithms such as finding shortest paths (Dijkstra’s algorithm) and critical paths (the longest path problem) in an acyclic graph.

In this article, we will discuss the topological sort algorithm and some of the common interview questions that you may be asked about it.

Here are 20 commonly asked Topological Sort interview questions and answers to prepare you for your interview:

A Topological Sort is a way of ordering a set of elements such that if element A comes before element B, then all of the elements that A depends on will also come before B. This is often used as a way of ensuring that dependencies are satisfied before proceeding with a particular task.

A topological sort algorithm is used to order a set of vertices in a directed acyclic graph. The algorithm starts by visiting each vertex in the graph and marking it as visited. It then visits each of the unvisited vertices and marks them as visited. Finally, it outputs the list of visited vertices in topological order.

A DFS based algorithm will visit all of the nodes in the graph before moving on to the next node, while a BFS based algorithm will visit the nodes in the order that they are found.

Topological sorts are often used in dependency management. For example, if you are managing a project with a lot of dependencies, you may use a topological sort to ensure that all of the dependencies are met before you can begin work on the project. This can help to avoid a lot of headaches down the road!

In order for a topological sort to be possible, the graph should be directed and acyclic. A directed graph is one in which the edges all have a specific direction, and an acyclic graph is one without any cycles (meaning there is no path from any vertex back to itself).

Yes, it is possible to run a topological sort on a directed acyclic graph. The way it works is that you first find a vertex that has no incoming edges and you label it with the number 1. Then, you find all of the vertices that have an edge coming from that vertex and you label them with the number 2. You continue this process until all of the vertices have been labeled. The order in which the vertices are labeled is the topological sort.

The best way to check if a cycle exists in a graph when performing a topological sort is to keep track of the vertices that have been visited. If a vertex is visited twice, then there is a cycle.

There are a few different ways to implement a topological sort, but the most common way is to use a depth-first search algorithm. You can find a detailed explanation and example of how to do this in Python here: https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/

The time complexity of a topological sort is O(V + E), where V is the number of vertices and E is the number of edges.

If we try to run a topological sort on an undirected graph, it will not work because topological sort only works on directed graphs. This is because an undirected graph does not have a direction associated with its edges, so there is no way to determine which order the vertices should be visited in.

A linear-time topological sort is one that can be done in O(n) time, where n is the number of vertices in the graph. A depth first search based topological sort is one that uses a depth first search to find the topological order of the vertices. The main difference is that a linear-time topological sort can be done in O(n) time, while a depth first search based topological sort can take up to O(n^2) time.

Kahn’s Algorithm is a topological sorting algorithm that works by first finding a set of “start nodes” – nodes with no incoming edges – and then removing them from the graph, one at a time. For each node that is removed, we add its outgoing edges to a queue. Once all the start nodes have been removed, we take the next node in the queue (which will now have no incoming edges) and repeat the process.

There are a few other algorithms that can be used for topological sort, but Kahn’s algorithm is generally considered to be the most efficient. One alternative is the Depth-First Search (DFS) algorithm, which can be used to find the topological order of a graph. However, DFS can be less efficient than Kahn’s algorithm, particularly if the graph is large and/or dense.

The advantage of using a stack is that it allows you to process the vertices in the order that they are finished, which is the reverse of the order in which they are finished. This is helpful because it means that you can process the vertices in the order that they are needed, which can make the algorithm more efficient.

If there are cycles in the graph, then it is impossible to find a topological ordering. This is because a topological ordering must put all the vertices in a linear order, but if there are cycles then there is no linear order that can satisfy all the edges.

Topological sorts are important because they can be used to find the order in which a set of tasks need to be completed. For example, if you have a set of tasks that need to be completed in order to finish a project, a topological sort can be used to find the order in which those tasks need to be completed. This can be helpful in ensuring that all of the necessary tasks are completed in the correct order and that no task is missed.

A DAG is a graph with no cycles. That is, there is no way to start at any node and follow a path that eventually loops back to the starting node. DAGs are often used to model situations where there is a partial order between elements. For example, a topological sort of a DAG G can be used to order the vertices of G so that if there is an edge from u to v, then u appears before v in the ordering.

One way to avoid deadlocks is to use a technique called lock ordering. With lock ordering, you establish a strict order in which locks can be acquired. Tasks must acquire all locks in the correct order, and they must release locks in the reverse order. This ensures that no two tasks can ever deadlock each other, because they will always be acquiring and releasing locks in the same order.

A dependency graph is a graph that represents the dependencies between different tasks. A precedence graph is a graph that represents the order in which those tasks must be completed. In a dependency graph, the edges represent the dependencies between the tasks, while in a precedence graph, the edges represent the order in which the tasks must be completed.

The time complexity of an iterative version of a topological sort is O(V + E), while the time complexity of a recursive version is O(V + E).