Graph coding interview questions 9 questions in 1 chapter

Nine graph coding questions in Java and Python, from islands and rotting oranges to topological sort, Dijkstra and division queries.

Part of Applied AI Engineer (LLM) coding learning path

  1. 01Grid and graph traversal9 questions

Grid and graph traversal

Deep copies, connected land, and a multi-source infection.

  • 1.1 Clone Graph Medium 4 min

    I walk the graph with a hash map from the original node to its clone. The first time I see a node I create its copy, store it in the map, then recurse or queue through its neighbours and attach the cloned neighbours. The map both avoids cycles and gives me the already-built copies.

    Read the full answer
  • 1.2 Number of Islands Medium 4 min

    I scan the grid, and every time I find a land cell I increment the island count and flood-fill that cell and all connected land to water, or to a visited mark, with DFS or BFS. Each island is counted once because its cells are consumed during the fill.

    Read the full answer
  • 1.3 Rotting Oranges Medium 4 min

    I put every already-rotten orange into a queue and run a multi-source BFS. Each minute I rot the fresh neighbours of everything currently in the queue. If any fresh orange remains when the queue is empty, I return -1, otherwise I return the number of minutes, which is the last depth I reached.

    Read the full answer
  • 1.4 Number of Provinces Medium 3 min

    The matrix is an undirected adjacency matrix of cities. I walk each unvisited city with DFS or BFS, or I union every connected pair and count the number of roots. Each connected component is one province.

    Read the full answer
  • 1.5 Course Schedule Medium 3 min

    This is a cycle check on a directed graph of course prerequisites. I build the adjacency list and incoming degrees, then I queue every course with degree zero and reduce the degrees of its neighbours. If I process every course, a topological order exists and you can finish; if not, there is a cycle.

    Read the full answer
  • 1.6 Course Schedule II Medium 4 min

    I run the same Kahn topological sort as Course Schedule, but I also record the order of the courses I take. If I cannot take every course, I return an empty array because a cycle makes a valid order impossible. Any valid topological order is accepted.

    Read the full answer
  • 1.7 Minimum Height Trees Medium 4 min

    The roots of the minimum-height trees are the centroids of the undirected tree, so there are one or two of them. I repeatedly peel off the current leaves, like a topological sort from the outside in, until one or two nodes remain. Those leftover nodes are the answer.

    Read the full answer
  • 1.8 Network Delay Time Medium 4 min

    This is a shortest-path problem on a weighted directed graph. I run Dijkstra from node k with a min-heap of (time, node), and I keep the best arrival time for every node. The answer is the maximum of those arrival times, or -1 if some node is never reached.

    Read the full answer
  • 1.9 Evaluate Division Medium 6 min

    Each equation a / b = v is a weighted directed edge a to b with weight v, and b to a with weight 1/v. For a query I BFS or DFS from the numerator, multiplying the edge weights along the path, and I return the product when I reach the denominator, or -1 if they are disconnected or missing.

    Read the full answer