Coding challenges
Now that we have had a brief overview of trees and graphs, it is time to challenge ourselves with the 25 most popular coding problems encountered in interviews about these topics.
As usual, we have a mix of problems that are usually encountered by the top companies of the world, including IT giants such as Amazon, Adobe and Google. So, let's get started!
Coding challenge 1 – Paths between two nodes
Problem: Consider you've been given a directed graph. Write a snippet of code that returns true
if there is a path (route) between two given nodes.
Solution: Let's consider the directed graph shown in the following diagram:
If we consider nodes D and E, then we can see that from D to E, there are three paths, while from E to D, there are none. So, if we start from D and traverse the graph (via BFS or DFS) then, at some point, we have to pass through node E,...