Challenge - 5 Problems
Graph Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency list for directed weighted graph
What is the printed adjacency list representation of the directed weighted graph after adding edges?
DSA C#
using System; using System.Collections.Generic; class Graph { public Dictionary<int, List<(int, int)>> adjList = new Dictionary<int, List<(int, int)>>(); public void AddEdge(int u, int v, int w) { if (!adjList.ContainsKey(u)) adjList[u] = new List<(int, int)>(); adjList[u].Add((v, w)); } public void PrintGraph() { foreach (var vertex in adjList) { Console.Write(vertex.Key + " -> "); foreach (var edge in vertex.Value) { Console.Write($"({edge.Item1}, {edge.Item2}) "); } Console.WriteLine(); } } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(1, 2, 5); g.AddEdge(1, 3, 10); g.AddEdge(2, 3, 2); g.PrintGraph(); } }
Attempts:
2 left
💡 Hint
Check the order and weights of edges added from vertex 1 and 2.
✗ Incorrect
The graph is directed and weighted. Edges added from 1 to 2 with weight 5, from 1 to 3 with weight 10, and from 2 to 3 with weight 2. The adjacency list prints edges in the order they were added.
❓ Predict Output
intermediateOutput of adjacency matrix for undirected unweighted graph
What is the printed adjacency matrix after adding edges to the undirected graph?
DSA C#
using System; class Graph { private int[,] matrix; private int size; public Graph(int n) { size = n; matrix = new int[n, n]; } public void AddEdge(int u, int v) { matrix[u, v] = 1; matrix[v, u] = 1; } public void PrintMatrix() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } } } class Program { static void Main() { Graph g = new Graph(4); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(1, 3); g.PrintMatrix(); } }
Attempts:
2 left
💡 Hint
Remember undirected means symmetric matrix entries.
✗ Incorrect
Edges added between 0-1, 0-2, and 1-3. Matrix is symmetric with 1s at these positions and 0s elsewhere.
🧠 Conceptual
advancedIdentify graph type from description
Which graph type is described as having edges with directions and weights representing distances?
Attempts:
2 left
💡 Hint
Edges have directions and weights.
✗ Incorrect
Directed weighted graph has edges with direction and weights like distances.
❓ Predict Output
advancedOutput of BFS traversal on undirected graph
What is the order of vertices visited in BFS starting from vertex 0?
DSA C#
using System; using System.Collections.Generic; class Graph { private 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>(); if (!adjList.ContainsKey(v)) adjList[v] = new List<int>(); adjList[u].Add(v); adjList[v].Add(u); } public List<int> BFS(int start) { List<int> visitedOrder = new List<int>(); Queue<int> queue = new Queue<int>(); HashSet<int> visited = new HashSet<int>(); queue.Enqueue(start); visited.Add(start); while (queue.Count > 0) { int current = queue.Dequeue(); visitedOrder.Add(current); foreach (int neighbor in adjList[current]) { if (!visited.Contains(neighbor)) { visited.Add(neighbor); queue.Enqueue(neighbor); } } } return visitedOrder; } } class Program { static void Main() { Graph g = new Graph(); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(1, 3); g.AddEdge(2, 3); var order = g.BFS(0); Console.WriteLine(string.Join(", ", order)); } }
Attempts:
2 left
💡 Hint
BFS visits neighbors level by level.
✗ Incorrect
Starting at 0, neighbors 1 and 2 are visited first, then their neighbor 3.
🧠 Conceptual
expertEdge count in complete undirected graph
How many edges are there in a complete undirected graph with 7 vertices?
Attempts:
2 left
💡 Hint
Use formula n*(n-1)/2 for edges in complete undirected graph.
✗ Incorrect
Complete undirected graph edges count is n*(n-1)/2 = 7*6/2 = 21.
