Challenge - 5 Problems
Adjacency List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency list after adding edges
What is the printed adjacency list after running the following C# code?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adjList = new Dictionary<int, List<int>>(); public void AddEdge(int u, int v) { if (!adjList.ContainsKey(u)) adjList[u] = new List<int>(); adjList[u].Add(v); } public void PrintGraph() { foreach (var node in adjList) { Console.Write(node.Key + " -> "); foreach (var neighbor in node.Value) { Console.Write(neighbor + " -> "); } Console.WriteLine("null"); } } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2); g.AddEdge(1, 3); g.AddEdge(2, 4); g.AddEdge(3, 4); g.PrintGraph(); } }
Attempts:
2 left
💡 Hint
Check the order in which edges are added to each node's adjacency list.
✗ Incorrect
The adjacency list stores neighbors in the order edges are added. Node 1 has neighbors 2 and 3, node 2 has neighbor 4, and node 3 has neighbor 4.
❓ Predict Output
intermediateAdjacency list after adding undirected edges
What is the output adjacency list after running this C# code that adds undirected edges?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adjList = new Dictionary<int, List<int>>(); public void AddUndirectedEdge(int u, int v) { if (!adjList.ContainsKey(u)) adjList[u] = new List<int>(); if (!adjList.ContainsKey(v)) adjList[v] = new List<int>(); adjList[u].Add(v); adjList[v].Add(u); } public void PrintGraph() { foreach (var node in adjList) { Console.Write(node.Key + " -> "); foreach (var neighbor in node.Value) { Console.Write(neighbor + " -> "); } Console.WriteLine("null"); } } } class Program { static void Main() { Graph g = new Graph(); g.AddUndirectedEdge(1, 2); g.AddUndirectedEdge(1, 3); g.AddUndirectedEdge(2, 4); g.PrintGraph(); } }
Attempts:
2 left
💡 Hint
Remember undirected edges add neighbors both ways.
✗ Incorrect
Each undirected edge adds neighbors to both nodes. Node 1 connects to 2 and 3, node 2 connects to 1 and 4, etc.
🔧 Debug
advancedIdentify the error in adjacency list insertion
What error will this C# code produce when adding edges to the adjacency list?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adjList = new Dictionary<int, List<int>>(); public void AddEdge(int u, int v) { adjList[u].Add(v); } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2); } }
Attempts:
2 left
💡 Hint
Check if the dictionary contains the key before adding to the list.
✗ Incorrect
The code tries to add to a list at key 1 without initializing the list first, causing a KeyNotFoundException.
❓ Predict Output
advancedAdjacency list after removing an edge
What is the adjacency list output after removing edge 1->3 from this graph?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adjList = new Dictionary<int, List<int>>(); public void AddEdge(int u, int v) { if (!adjList.ContainsKey(u)) adjList[u] = new List<int>(); adjList[u].Add(v); } public void RemoveEdge(int u, int v) { if (adjList.ContainsKey(u)) { adjList[u].Remove(v); } } public void PrintGraph() { foreach (var node in adjList) { Console.Write(node.Key + " -> "); foreach (var neighbor in node.Value) { Console.Write(neighbor + " -> "); } Console.WriteLine("null"); } } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2); g.AddEdge(1, 3); g.AddEdge(2, 4); g.RemoveEdge(1, 3); g.PrintGraph(); } }
Attempts:
2 left
💡 Hint
Removing edge 1->3 deletes 3 from node 1's neighbors.
✗ Incorrect
After removal, node 1's adjacency list no longer contains 3, only 2 remains.
❓ Predict Output
expertOutput after BFS traversal using adjacency list
What is the order of nodes printed by this BFS traversal starting from node 1?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<int>> adjList = new Dictionary<int, List<int>>(); public void AddEdge(int u, int v) { if (!adjList.ContainsKey(u)) adjList[u] = new List<int>(); adjList[u].Add(v); } public void BFS(int start) { Queue<int> queue = new Queue<int>(); HashSet<int> visited = new HashSet<int>(); queue.Enqueue(start); visited.Add(start); while (queue.Count > 0) { int node = queue.Dequeue(); Console.Write(node + " "); if (adjList.ContainsKey(node)) { foreach (var neighbor in adjList[node]) { if (!visited.Contains(neighbor)) { queue.Enqueue(neighbor); visited.Add(neighbor); } } } } } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2); g.AddEdge(1, 3); g.AddEdge(2, 4); g.AddEdge(3, 5); g.AddEdge(4, 6); g.BFS(1); } }
Attempts:
2 left
💡 Hint
BFS visits neighbors level by level in the order they appear in adjacency list.
✗ Incorrect
BFS starts at 1, visits neighbors 2 and 3, then their neighbors 4, 5, and finally 6.
