Challenge - 5 Problems
Graph Mastery: Understanding Why Trees Fall Short
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Graph Representation with Cycles
Consider the following C# code that creates a graph with cycles. What will be the printed adjacency list after adding all edges?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adj = new Dictionary<int, List<int>>(); public void AddEdge(int u, int v) { if (!adj.ContainsKey(u)) adj[u] = new List<int>(); adj[u].Add(v); } public void PrintGraph() { foreach (var node in adj) { Console.Write(node.Key + " -> "); foreach (var neighbor in node.Value) { Console.Write(neighbor + " "); } Console.WriteLine(); } } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2); g.AddEdge(2, 3); g.AddEdge(3, 1); // cycle here g.AddEdge(3, 4); g.PrintGraph(); } }
Attempts:
2 left
💡 Hint
Look carefully at all edges added, especially the one creating a cycle.
✗ Incorrect
The graph has edges 1->2, 2->3, 3->1 (cycle), and 3->4. So node 3 has neighbors 1 and 4.
🧠 Conceptual
intermediateWhy Trees Cannot Model Cyclic Relationships
Which of the following statements best explains why trees cannot represent cyclic relationships?
Attempts:
2 left
💡 Hint
Think about the definition of a tree and what a cycle means.
✗ Incorrect
By definition, trees are acyclic connected graphs. Cycles violate the tree property.
🔧 Debug
advancedIdentifying the Error in Tree vs Graph Modeling
Given the following code snippet intended to model a graph with cycles, what error will occur if we try to use a TreeNode class designed for trees?
DSA C#
class TreeNode { public int val; public List<TreeNode> children = new List<TreeNode>(); public TreeNode(int v) { val = v; } } // Attempt to create a cycle TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); a.children.Add(b); b.children.Add(a); // creates cycle
Attempts:
2 left
💡 Hint
Think about what happens if you traverse this structure recursively.
✗ Incorrect
The cycle causes infinite recursion during traversal, leading to stack overflow.
🚀 Application
advancedChoosing Data Structure for Social Network Modeling
You want to model a social network where users can follow each other, including mutual follows (cycles). Which data structure is best suited for this?
Attempts:
2 left
💡 Hint
Consider if cycles and multiple connections are possible in the network.
✗ Incorrect
Graphs can represent complex relationships including cycles and multiple edges, unlike trees.
❓ Predict Output
expertOutput of Graph Traversal Showing Cycle Detection
What will be the output of the following C# code that performs DFS on a graph with a cycle and detects it?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adj = new Dictionary<int, List<int>>(); public void AddEdge(int u, int v) { if (!adj.ContainsKey(u)) adj[u] = new List<int>(); adj[u].Add(v); } public bool DFS(int node, HashSet<int> visited, HashSet<int> recStack) { if (recStack.Contains(node)) { Console.WriteLine("Cycle detected at node " + node); return true; } if (visited.Contains(node)) return false; visited.Add(node); recStack.Add(node); if (adj.ContainsKey(node)) { foreach (var neighbor in adj[node]) { if (DFS(neighbor, visited, recStack)) return true; } } recStack.Remove(node); return false; } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2); g.AddEdge(2, 3); g.AddEdge(3, 4); g.AddEdge(4, 2); // cycle here bool hasCycle = g.DFS(1, new HashSet<int>(), new HashSet<int>()); if (!hasCycle) Console.WriteLine("No cycle detected"); } }
Attempts:
2 left
💡 Hint
Look at where the recursion stack detects a repeated node first.
✗ Incorrect
The cycle is detected when node 2 is revisited in the recursion stack.
