← Back to section

Final exam — Graphs

Three unseen problems (not covered in the lessons) to test whether you leave this section able to solve any problem of the topic: one easy, one medium and one hard. Some test cases are hidden — like the real LeetCode. Solve in Python or JavaScript.

Easy

Find if Path Exists in Graph

There are n nodes (0 to n-1) and undirected edges in edges. Return true if there is a path between source and destination.

Example: n=3, edges=[[0,1],[1,2],[2,0]], source=0, destination=2 → true.

Carregando editor…
Medium

Number of Provinces

Given an n × n matrix isConnected where isConnected[i][j] = 1 indicates that cities i and j are directly linked, return the number of provinces (groups of cities connected, directly or indirectly).

Example: [[1,1,0],[1,1,0],[0,0,1]] → 2.

Carregando editor…
Hard

Word Ladder

Given beginWord, endWord and a list wordList, transform beginWord into endWord by changing one letter at a time, with each intermediate word present in wordList. Return the number of words in the shortest sequence (including the two ends), or 0 if impossible.

Example: "hit" → "cog", list ["hot","dot","dog","lot","log","cog"] → 5 (hit→hot→dot→dog→cog).

Carregando editor…