Graphs Interview Questions

Graphs are an abstract data type made up of connected vertices and edges that allows operations on those connections. They are also used in other disciplines. For example, in epidemiology and public health research, we use graphs to model disease transmission between hosts. In this post, I'll focus on how we can use graph theory to model relationships between people and the things they care about.

Let's practice using them in this section!

Section Menu

How do I use this section?

1. CHALLENGE

Implement a Graph

Since a graph is one of the more difficult data structures to conceptualize in a programmatic 2D manner, let's go ahead and implement it! We'll go with an adjacency list version. Note that there's also the adjacency matrix method which we will cover later. <img src="https://storage.go...

2. CHALLENGE

Find Deletion Distance

Can you determine the deletion distance between two strings? Let's define the deletion distance as the numbers of characters needed to delete from two strings to make them equal. <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/multistep/find-deletion-distance/find-deletion-distance-4.png" class="img...

3. CHALLENGE

Levenshtein Edit Distance

An edit distance is a way to quantify how different two strings are. This is calculated using the minimum number of transformations to turn one string to another. The transformations include insertion, deletion, and substitution. So when comparing two identical strings, say cat and cat, the edit distance would be `...

4. CHALLENGE

Is This Graph a Tree

Given an undirected graph, can you see if it's a tree? If so, return true and false otherwise. An undirected graph is a tree base...

5. CHALLENGE

Most Strongly Connected

Challenge: Finding the Largest Island of Connected Ones in a Matrix Introduction Imagine you're an explorer on a digital ocean filled with islands made up of ones (1s) and water represented by zeroes (0s). Your task is to find the largest island composed of connected 1s in a given 2D grid (or matrix). Here's a samp...

6. CHALLENGE

Course Prerequisites

Here's a classic challenge that comes up in real-life interviews surprisingly often. Interviewers like it as a way to assess your ability to find the right data structure for a non-obvious and non-trivial use case. <img src="https://storage.googleapis.com/algodailyrandomassets/curriculum/graphs/course-prerequisites-cover-image.p...

7. CHALLENGE

Detect An Undirected Graph Cycle

Can you detect a cycle in an undirected graph? Recall that an undirected graph is one where the edges are bidirectional. A cycle is one where there is a closed path, that is, the first and last graph vertices can be the same. <img src="https://stor...

8. CHALLENGE

The Two Coloring Graph Problem

Question Given a graph, can you use two colors to color each node of the graph, such that no two adjacent nodes have the same color? The Problem The graph coloring problem is a well-known problem in computer science. It requires coloring different node...

9. CHALLENGE

Shortest Path Distance in Matrix

You are given a 2D matrix with several characters contained in its cells. You will also have two distinct characters which are guaranteed to be in the given matrix. Your job will be to find the minimum manhattan distance between the two characters in the matrix. The manhattan distance of...

10. CHALLENGE

Nested List Weight Sum

When preparing for an interview, you should be familiar with fundamental graph traversal techniques because they are frequently used in technical interviews. As we live in an interconnected society with no isolated piece of information, the use of graphs is becoming more widespread. Today's prompt has one such graph traversal t...