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 adding the edges in the given Java code?
DSA Java
import java.util.*; public class Graph { private Map<Integer, List<Integer>> adjList = new HashMap<>(); public void addEdge(int u, int v) { adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v); adjList.computeIfAbsent(v, k -> new ArrayList<>()); } public void printGraph() { for (int key : adjList.keySet()) { System.out.print(key + " -> "); for (int val : adjList.get(key)) { System.out.print(val + " -> "); } System.out.println("null"); } } public static void main(String[] args) { Graph g = new Graph(); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(2, 4); g.printGraph(); } }
Attempts:
2 left
💡 Hint
Look carefully at how edges are added and which nodes get neighbors.
✗ Incorrect
The addEdge method adds v to u's list and ensures v exists in the map with an empty list. It does not add u to v's list, so edges are one-way.
❓ Predict Output
intermediateOutput after adding undirected edges
What is the output of the adjacency list after adding edges with the undirected addEdge method?
DSA Java
import java.util.*; public class Graph { private Map<Integer, List<Integer>> adjList = new HashMap<>(); public void addEdge(int u, int v) { adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v); adjList.computeIfAbsent(v, k -> new ArrayList<>()).add(u); } public void printGraph() { for (int key : adjList.keySet()) { System.out.print(key + " -> "); for (int val : adjList.get(key)) { System.out.print(val + " -> "); } System.out.println("null"); } } public static void main(String[] args) { Graph g = new Graph(); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(2, 4); g.printGraph(); } }
Attempts:
2 left
💡 Hint
Edges are added both ways for undirected graphs.
✗ Incorrect
Since addEdge adds v to u's list and u to v's list, neighbors appear in both nodes' lists.
🔧 Debug
advancedIdentify the error in adjacency list initialization
What error will this Java code produce when trying to add edges to the adjacency list?
DSA Java
import java.util.*; public class Graph { private Map<Integer, List<Integer>> adjList = new HashMap<>(); public void addEdge(int u, int v) { adjList.get(u).add(v); adjList.get(v).add(u); } public static void main(String[] args) { Graph g = new Graph(); g.addEdge(1, 2); } }
Attempts:
2 left
💡 Hint
Check if the adjacency list has entries before calling get(u).add(v).
✗ Incorrect
The map is empty initially, so adjList.get(u) returns null, causing NullPointerException when calling add.
🧠 Conceptual
advancedMemory efficiency of adjacency list vs adjacency matrix
Which statement correctly explains why adjacency lists are more memory efficient than adjacency matrices for sparse graphs?
Attempts:
2 left
💡 Hint
Think about how each structure stores edges for nodes that have few connections.
✗ Incorrect
Adjacency lists only keep neighbors for nodes that have edges, saving space when many edges are missing.
❓ Predict Output
expertOutput after removing an edge from adjacency list
What is the output of the adjacency list after removing edge (1, 3) from the undirected graph?
DSA Java
import java.util.*; public class Graph { private Map<Integer, List<Integer>> adjList = new HashMap<>(); public void addEdge(int u, int v) { adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v); adjList.computeIfAbsent(v, k -> new ArrayList<>()).add(u); } public void removeEdge(int u, int v) { adjList.getOrDefault(u, new ArrayList<>()).remove(Integer.valueOf(v)); adjList.getOrDefault(v, new ArrayList<>()).remove(Integer.valueOf(u)); } public void printGraph() { for (int key : adjList.keySet()) { System.out.print(key + " -> "); for (int val : adjList.get(key)) { System.out.print(val + " -> "); } System.out.println("null"); } } public static void main(String[] args) { 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) removes 3 from 1's list and 1 from 3's list.
✗ Incorrect
After removal, 1's neighbors are only 2, and 3 has no neighbors. Other edges remain unchanged.
