# Graph data structure cheat sheet for coding interviews

169

If you are looking for a job in tech/programming, you will certainly have to face a coding/technical round. This is the most crucial interview round that employers conduct in order to hire candidates.

The goal of these interviews is to find out how well you can think on your feet, work in a team, and solve problems.

With so many companies looking for the best talent, technical interviews are becoming more and more common.

For all the base-level programmers out there, test your coding skills by solving the graph interview questions mentioned at the end of this blog!

## What is Graph Data Structure?

A graph is a collection of objects with a set of relationships between the objects. Graphs can be represented visually by drawing them, or by using a software program.

Graphs are used in many fields including computer science engineering, and social sciences. A graph consists of nodes, which are the objects in the graph and edges.

Now, we shall talk business! Did you know that graph data structure is one of the most important topics in coding interviews?

Check out the following section for some of the top coding questions on Graph Data Structure that you will encounter in your coding placement interviews.

## How to prepare Graphs for Coding Interviews?

Ready to solve a few questions on Graphs data structure? Let’s have a look:

### Breadth First Traversal in a given Graph

If you are familiar with the concept of Breadth First Traversal in a Tree then this concept is closely related to that.

The only difference is that unlike the structure for the trees, the graphs may consist of cycles within the data structure.

Refer to the following problem statement and answer key to better understand this concept:

Problem Statement:

Q. Traverse the given Graph and figure out the position of the two adjacent vertices.

• Declare a cue and insert a start node.
• Initialize the visited array and mark the starting node as visited.
• Follow the process below until the queue is empty.
• In the queue, remove the first vertex.
• Mark this vertex as visited.
• Queue all unvisited neighbors of the node.

### Depth-First Search in a Graph

Graph depth traversal (or search) is similar to tree depth traversal (DFS). The only problem is that, unlike trees, graphs can contain cycles, so a node can be visited twice.

To avoid processing a node multiple times, use a boolean visited array.

Check out the following problem statement related to this question.

Problem Statement:

Q. Perform a Depth First Traversal in a Graph.

Answer Key: The algorithm starts at the root node (for graphs, choose any node as the root node) and explores as much as possible along each branch before tracing back.

So the basic idea is to start from the root or any node, mark a node, move to an adjacent unmarked node, and continue this loop until there are no more unmarked adjacent nodes.

Then go back and look for other unmarked nodes and traverse them. Finally, print the nodes in the path.

### Adjacent List in a graph

If you are looking for a way to graph two lists of numbers, you could use an adjacent list. To do this, you would put the first number in the left column and the second number in the right column.

You then would draw a line from the left column to the right column.

Check out the following question based on Adjacent List in a graph:

Problem Statement:

Q. Figure out the adjacent list in a given graph.

Answer Key: You can also use a dictionary where the keys represent nodes and the values bare lists of neighbors.

This is useful when nodes are represented by strings or objects or are not well-mapped to list indices.

## The shortest path between two nodes in a Graph

The shortest path between two nodes in a graph can be found by traversing the graph from one node to the other and then taking the shortest distance.

It is important to know the difference between a path and a route:

• A path is a series of nodes that are connected.
• A route is a path that has been followed.

Let’s have a look at the following question based on the shortest path between two nodes in a graph.

Problem Statement:

Q. Given a graph, figure out the shortest paths between two given nodes where the graph is undirected and unweighted.

Answer Key: Let’s say you want to find the shortest path from Node A to Node B. You could do a breadth-first search that starts at Node A and ends at Node B. Or you could do a depth-first search that starts at Node B and ends at Node A.

The difference between these two algorithms is that the breadth-first algorithm starts at the first node and then goes to the next node, while the depth-first algorithm starts at the last node and goes to the first node.

## Bidirectional Search in a Graph

A bidirectional search is a graph search algorithm that finds the shortest path from the source to the goal vertex. The algorithm is used in routing problems and graph theory to find the shortest path from one vertex to another.

Finally, check out the following problem statement based on a Bidirectional Search in a Graph:

Problem Statement:

Q. Given a graph, perform a bidirectional search.

Answer Key: Suppose we have to figure out if there is a path within the graph between two vertices 0 and 14. Now we can perform two searches, one from vertex 0 and one from vertex 14.

If both the forward and backward searches match at vertex 7, we know we found a path from vertices 0 to 14 and we can terminate the search. You can clearly see the success in avoiding unnecessary searches.

Final Thoughts

Interested in solving the Amazon Online Test Questions, Google Online Assessment Questions, and Fintech Interview Questions on a single get-go? Start by solving our basic level Graph Data Structure Questions and pave your way to the top!

With that, we wish you all the best for your next coding interview, and may you land the job of your dreams!

Previous articleWhat is Dynamic Programming : Properties & examples
Next articleMemoization and Fibonacci

This site uses Akismet to reduce spam. Learn how your comment data is processed.