Challenge - 5 Problems
Graph vs Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediateKey Structural Difference Between Graph and Tree
Which of the following best describes the key structural difference between a graph and a tree?
Attempts:
2 left
💡 Hint
Think about connectivity and cycles in both structures.
✗ Incorrect
A tree is a special type of graph that is connected and has no cycles. Graphs are more general and can have cycles and may not be connected.
❓ Predict Output
intermediateOutput of Graph vs Tree Edge Count
What is the output of the following C# code that counts edges in a tree and a graph?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int nodes = 5; int treeEdges = nodes - 1; int graphEdges = 7; Console.WriteLine($"Tree edges: {treeEdges}"); Console.WriteLine($"Graph edges: {graphEdges}"); } }
Attempts:
2 left
💡 Hint
Recall that a tree with n nodes has exactly n-1 edges.
✗ Incorrect
A tree with 5 nodes has 4 edges. The graph edges count is given as 7.
🔧 Debug
advancedIdentify the Error in Tree Cycle Check Code
What error will the following C# code produce when checking if a tree has a cycle?
DSA C#
using System; using System.Collections.Generic; class Program { static bool HasCycle(Dictionary<int, List<int>> graph, int node, int parent, HashSet<int> visited) { visited.Add(node); foreach (var neighbor in graph[node]) { if (!visited.Contains(neighbor)) { if (HasCycle(graph, neighbor, node, visited)) return true; } else if (neighbor != parent) { return true; } } return false; } static void Main() { var graph = new Dictionary<int, List<int>> { {1, new List<int>{2}}, {2, new List<int>{1, 3}}, {3, new List<int>{2, 4}}, {4, new List<int>{3, 5}}, {5, new List<int>{4}} }; var visited = new HashSet<int>(); Console.WriteLine(HasCycle(graph, 1, -1, visited)); } }
Attempts:
2 left
💡 Hint
Check if the graph has any cycles.
✗ Incorrect
The given graph is a tree (no cycles). The code correctly detects no cycles and returns false.
❓ Predict Output
advancedOutput of Adding Edge Creating Cycle in Graph
What will be the output after adding an edge that creates a cycle in the graph?
DSA C#
using System; using System.Collections.Generic; class Program { static bool HasCycle(Dictionary<int, List<int>> graph, int node, int parent, HashSet<int> visited) { visited.Add(node); foreach (var neighbor in graph[node]) { if (!visited.Contains(neighbor)) { if (HasCycle(graph, neighbor, node, visited)) return true; } else if (neighbor != parent) { return true; } } return false; } static void Main() { var graph = new Dictionary<int, List<int>> { {1, new List<int>{2}}, {2, new List<int>{1, 3}}, {3, new List<int>{2, 4}}, {4, new List<int>{3, 5}}, {5, new List<int>{4}} }; // Add edge creating cycle between 5 and 1 graph[5].Add(1); graph[1].Add(5); var visited = new HashSet<int>(); Console.WriteLine(HasCycle(graph, 1, -1, visited)); } }
Attempts:
2 left
💡 Hint
Adding an edge between 5 and 1 creates a cycle.
✗ Incorrect
The added edge creates a cycle in the graph, so the cycle detection returns true.
🧠 Conceptual
expertWhy Trees Have Exactly One Path Between Any Two Nodes
Why does a tree have exactly one path between any two nodes?
Attempts:
2 left
💡 Hint
Think about connectivity and cycles in trees.
✗ Incorrect
In a tree, being connected and having no cycles means there is only one unique path between any two nodes.
