All puzzles › Graphs

Graphs Practice Problems

24 puzzles (13 medium, 11 hard), 3 free, in Python, JavaScript and C++.

A graph is things and the connections between them: cities and roads, courses and prerequisites, cells in a grid and their neighbors. The core skills are exploring one without visiting anything twice, finding the shortest way between two points, and ordering tasks that depend on each other.

How to spot a graphs problem

Any time the problem describes relationships, dependencies, or a grid you move around in, it is a graph even if nobody says so. Breadth-first search gives the fewest steps when every step costs the same. Depth-first search is the tool for reachability and for detecting cycles. Weighted edges need Dijkstra.

New to it? Read the lesson first: BFS & DFS.

The puzzles, easiest first

  1. Walk Toward the Query (Medium, free): Vector databases do not compare a query with every stored vector.
  2. Are All the Rooms Connected? (Medium): A dungeon generator has made n rooms and dug some corridors, each joining two rooms.
  3. BFS Distances (Medium): Given a graph as an adjacency list and a start node, return how many hops each node is from the start, or -1 if you can't reach it.
  4. Detect Dependency Cycle (Medium): Given a directed graph as an adjacency list, return true if it has a cycle.
  5. Flood Fill (Medium): Given a grid, a starting cell, and a new color, recolor the starting cell and every same-colored cell connected to it, up, down, left, or right.
  6. How Many Hops (Medium): Given a network as an adjacency list and two routers, return the fewest hops between them, or -1 if there's no path.
  7. How Many Islands of Routers (Medium): Given a network as an adjacency list, return how many separate pieces it's split into.
  8. Is Target Reachable? (Medium): Given a directed graph as an adjacency list, a start, and a target, return true if there's any path from start to target.
  9. Max Area of Island (Medium): Given a grid of land ("1") and water ("0"), return the size of the biggest island, counted in cells.
  10. Number of Islands (Medium): Given a grid of land ("1") and water ("0"), return how many separate islands there are.
  11. Research Order (Medium): A strategy game's tech tree has n technologies, and each pair [tech, pre] means tech can't be researched until pre is done.
  12. What Else Goes Down With It (Medium): Given a list where deps[i] is the services that service i needs, and one service that just failed, return how many services end up down, counting the one that failed.
  13. What Nobody Is Holding (Medium): Given objects as an adjacency list of references and a list of root objects, return how many objects can't be reached from any root.
  14. How Many Rounds of Tool Calls (Hard, free): An agent has n tool calls to make, numbered from 0, and some need another call's result first.
  15. Uncross the Route (Hard, free): The best tour through a set of cities is too hard to find exactly, so solvers improve a tour they already have.
  16. Chase the Player (Hard): Given a grid where 0 is a wall and any other number is what it costs to step onto that tile (grass 1, swamp 5), return the cheapest cost from the top-left corner to the bottom-right, moving up, down, left or right, or -1 if there's no way through.
  17. Dijkstra Shortest Path (Hard): Given a weighted graph, where each node lists [neighbor, cost] pairs, return the cheapest total cost from src to target, or -1 if you can't get there.
  18. Held by Each Other (Hard): Given objects as an adjacency list of references and a list of roots, return how many objects are still allocated under reference counting.
  19. Nobody Can Move (Hard): Given a wait-for graph as an adjacency list, where an edge from A to B means A is waiting on B, return true if the system is deadlocked.
  20. Round and Round (Hard): Given a forwarding table where next[i] is the router that i sends packets to, or -1 if i delivers them itself, return true if any packet could go around in circles forever.
  21. Shortest Path (BFS) (Hard): Given a grid where 0 is open and 1 is a wall, a start, and an end, return the fewest steps from start to end, moving up, down, left, or right.
  22. The Cheapest Route (Hard): Given the number of routers, a list of two-way links as [a, b, cost], and two routers, return the cheapest total cost between them, or -1 if you can't get there.
  23. Topological Order (Hard): Given a directed graph as an adjacency list, return an order where every node comes before the nodes it points to.
  24. What Starts First (Hard): Given a list where deps[i] is the services that must start before service i, return an order to start them all, or [] if the dependencies loop.

Practice another pattern

Or browse by subject

Browse all 676 puzzles