Challenge - 5 Problems
Graph Representation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency matrix representation
What is the printed adjacency matrix after adding edges between nodes 0-1, 0-2, and 1-2 in an undirected graph with 3 nodes?
DSA C++
#include <iostream> int main() { int graph[3][3] = {0}; graph[0][1] = 1; graph[1][0] = 1; graph[0][2] = 1; graph[2][0] = 1; graph[1][2] = 1; graph[2][1] = 1; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { std::cout << graph[i][j] << " "; } std::cout << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Think about how undirected edges are represented symmetrically in the matrix.
✗ Incorrect
In an undirected graph, edges are bidirectional, so the adjacency matrix is symmetric. The edges between nodes 0-1, 0-2, and 1-2 set those positions to 1 in both directions.
🧠 Conceptual
intermediateChoosing adjacency list vs matrix for sparse graph
Which data structure is better for representing a graph with 1000 nodes but only 10 edges?
Attempts:
2 left
💡 Hint
Think about memory usage when most nodes have very few edges.
✗ Incorrect
Adjacency lists store only existing edges, so they use less memory for sparse graphs. Adjacency matrices always allocate space for all possible edges, which is wasteful here.
❓ Predict Output
advancedOutput of adjacency list after adding edges
What is the printed adjacency list after adding edges 0->1, 0->2, and 1->2 in a directed graph with 3 nodes?
DSA C++
#include <iostream> #include <vector> int main() { std::vector<std::vector<int>> adj(3); adj[0].push_back(1); adj[0].push_back(2); adj[1].push_back(2); for(int i=0; i<3; i++) { std::cout << i << ": "; for(int v : adj[i]) { std::cout << v << " -> "; } std::cout << "null" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Edges are directed and added only in one direction.
✗ Incorrect
Edges 0->1 and 0->2 add nodes 1 and 2 to adjacency list of 0. Edge 1->2 adds node 2 to adjacency list of 1. Node 2 has no outgoing edges.
🧠 Conceptual
advancedTime complexity for edge lookup
Which data structure provides O(1) time complexity to check if an edge exists between two nodes in a graph?
Attempts:
2 left
💡 Hint
Think about direct access to edge information by indices.
✗ Incorrect
Adjacency matrix uses a 2D array where each cell directly shows if an edge exists, so lookup is O(1). Adjacency lists require searching through neighbors, which is O(k) where k is degree.
🚀 Application
expertChoosing graph representation for dense graph algorithms
You need to implement an algorithm that frequently checks if an edge exists between any two nodes in a graph with 500 nodes and 120,000 edges. Which representation is best and why?
Attempts:
2 left
💡 Hint
Consider the trade-off between memory and speed for edge existence queries.
✗ Incorrect
For dense graphs with many edges, adjacency matrix uses more memory but allows O(1) edge existence checks, which is critical for frequent queries. Adjacency lists require searching neighbors, slower for dense graphs.
