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++
#include <iostream> #include <vector> using namespace std; int main() { int V = 3; vector<int> adj[V]; adj[0].push_back(1); adj[1].push_back(2); adj[2].push_back(0); for (int i = 0; i < V; i++) { cout << i << ": "; for (auto v : adj[i]) { cout << v << " -> "; } cout << "null" << endl; } return 0; }
Attempts:
2 left
💡 Hint
Check which vertices are connected to each vertex by the push_back calls.
✗ Incorrect
The code adds edges 0->1, 1->2, and 2->0. So adjacency list for 0 has 1, for 1 has 2, and for 2 has 0.
🧠 Conceptual
intermediateNumber of edges in adjacency list
If an undirected graph with 4 vertices has the following adjacency list representation:
0: 1 -> 2 -> null
1: 0 -> 3 -> null
2: 0 -> null
3: 1 -> null
How many edges does the graph have?
Attempts:
2 left
💡 Hint
Each edge appears twice in an undirected graph adjacency list.
✗ Incorrect
Counting all neighbors: 0 has 2, 1 has 2, 2 has 1, 3 has 1. Total 6 entries. Each edge counted twice, so edges = 6/2 = 3.
🔧 Debug
advancedIdentify the bug in adjacency list initialization
What error will this C++ code produce when compiled and run?
#include
#include
using namespace std;
int main() {
int V = 3;
vector adj;
adj[0].push_back(1);
return 0;
}
Attempts:
2 left
💡 Hint
Check how adj is declared and used.
✗ Incorrect
adj is a vector, not a vector>. adj[0] returns int&. int has no push_back member, causing compilation error.
❓ Predict Output
advancedOutput after adding edges in directed graph
What is the output of this C++ code printing adjacency list for a directed graph?
DSA C++
#include <iostream> #include <vector> using namespace std; int main() { int V = 4; vector<int> adj[V]; adj[0].push_back(1); adj[0].push_back(2); adj[1].push_back(2); adj[2].push_back(3); for (int i = 0; i < V; i++) { cout << i << ": "; for (auto v : adj[i]) { cout << v << " -> "; } cout << "null" << endl; } return 0; }
Attempts:
2 left
💡 Hint
Check which edges are added from each vertex.
✗ Incorrect
Edges added: 0->1, 0->2, 1->2, 2->3. So adjacency list matches option A.
🧠 Conceptual
expertMemory complexity of adjacency list vs adjacency matrix
For a graph with V vertices and E edges, which statement about memory usage is correct?
Attempts:
2 left
💡 Hint
Think about how edges are stored in each representation.
✗ Incorrect
Adjacency list stores edges only, so O(V + E). Adjacency matrix stores all possible edges, so O(V^2).
