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 an undirected weighted graph
What is the printed adjacency list after running this C++ code for an undirected weighted graph with 3 vertices and edges (0-1, weight 5), (1-2, weight 3)?
DSA C++
#include <iostream> #include <vector> using namespace std; int main() { int V = 3; vector<vector<pair<int,int>>> adj(V); adj[0].push_back({1, 5}); adj[1].push_back({0, 5}); adj[1].push_back({2, 3}); adj[2].push_back({1, 3}); for (int i = 0; i < V; i++) { cout << i << ": "; for (auto &edge : adj[i]) { cout << "(" << edge.first << "," << edge.second << ") "; } cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Remember that undirected edges are added in both directions with the same weight.
✗ Incorrect
The adjacency list stores neighbors and weights. For undirected edges, both vertices list each other with the same weight.
❓ Predict Output
intermediateOutput of adjacency matrix for a directed graph
What is the printed adjacency matrix after running this C++ code for a directed graph with 3 vertices and edges 0->1 and 1->2?
DSA C++
#include <iostream> using namespace std; int main() { int V = 3; int adj[3][3] = {0}; adj[0][1] = 1; adj[1][2] = 1; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { cout << adj[i][j] << " "; } cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
In adjacency matrix for directed graphs, adj[i][j] = 1 means edge from i to j.
✗ Incorrect
Only edges 0->1 and 1->2 are set to 1, others remain 0.
🧠 Conceptual
advancedIdentify the graph type from description
Which of the following best describes a graph where edges have directions and weights?
Attempts:
2 left
💡 Hint
Edges have arrows and numbers representing cost or distance.
✗ Incorrect
Directed weighted graph means edges have direction and weights.
❓ Predict Output
advancedOutput of BFS traversal on a directed graph
What is the order of vertices printed by this BFS traversal starting from vertex 0?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; void bfs(int start, vector<vector<int>> &adj) { vector<bool> visited(adj.size(), false); queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int v = q.front(); q.pop(); cout << v << " "; for (int u : adj[v]) { if (!visited[u]) { visited[u] = true; q.push(u); } } } } int main() { vector<vector<int>> adj = { {1, 2}, {2}, {3}, {} }; bfs(0, adj); return 0; }
Attempts:
2 left
💡 Hint
BFS visits neighbors level by level in order they appear.
✗ Incorrect
Starting at 0, neighbors 1 and 2 are visited in order, then 3 from 2.
🧠 Conceptual
expertNumber of edges in a complete undirected weighted graph with 5 vertices
How many edges are there in a complete undirected weighted graph with 5 vertices?
Attempts:
2 left
💡 Hint
Complete undirected graph has edges between every pair of vertices without duplicates.
✗ Incorrect
Number of edges = n*(n-1)/2 = 5*4/2 = 10.
