Challenge - 5 Problems
Graph Terminology Master
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 representation of this undirected weighted graph after adding edges?
DSA Java
import java.util.*; class Graph { Map<Integer, List<int[]>> adjList = new HashMap<>(); void addEdge(int u, int v, int w) { adjList.putIfAbsent(u, new ArrayList<>()); adjList.putIfAbsent(v, new ArrayList<>()); adjList.get(u).add(new int[]{v, w}); adjList.get(v).add(new int[]{u, w}); } void printGraph() { for (int key : adjList.keySet()) { System.out.print(key + " -> "); for (int[] pair : adjList.get(key)) { System.out.print("(" + pair[0] + ", " + pair[1] + ") "); } System.out.println("null"); } } } public class Main { public static void main(String[] args) { 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
Remember that in an undirected graph, edges are added in both directions with the same weight.
✗ Incorrect
Each edge is stored twice: once from u to v and once from v to u with the same weight. So vertex 1 connects to 2 and 3, vertex 2 connects back to 1 and also to 3, and vertex 3 connects back to 1 and 2.
❓ Predict Output
intermediateOutput of adjacency list for a directed weighted graph
What is the printed adjacency list representation of this directed weighted graph after adding edges?
DSA Java
import java.util.*; class Graph { Map<Integer, List<int[]>> adjList = new HashMap<>(); void addEdge(int u, int v, int w) { adjList.putIfAbsent(u, new ArrayList<>()); adjList.putIfAbsent(v, new ArrayList<>()); adjList.get(u).add(new int[]{v, w}); } void printGraph() { for (int key : adjList.keySet()) { System.out.print(key + " -> "); for (int[] pair : adjList.get(key)) { System.out.print("(" + pair[0] + ", " + pair[1] + ") "); } System.out.println("null"); } } } public class Main { public static void main(String[] args) { 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
In a directed graph, edges go only one way from source to destination.
✗ Incorrect
Edges are stored only from the source vertex to the destination vertex with the given weight. No reverse edges are added.
🧠 Conceptual
advancedIdentify the type of graph from description
You have a graph where edges have directions and weights. Which of the following terms correctly describes this graph?
Attempts:
2 left
💡 Hint
Edges have arrows and numbers representing cost or distance.
✗ Incorrect
A graph with edges that have directions (arrows) and weights (numbers) is called a directed weighted graph.
🧠 Conceptual
advancedDifference between vertices and edges
Which statement correctly describes the difference between vertices and edges in a graph?
Attempts:
2 left
💡 Hint
Think of a graph like a map of cities and roads.
✗ Incorrect
Vertices are the points or nodes (like cities), and edges are the connections or links (like roads) between them.
🔧 Debug
expertIdentify the error in weighted directed graph edge addition
What error will this code produce when adding edges to a weighted directed graph?
DSA Java
import java.util.*; class Graph { Map<Integer, List<int[]>> adjList = new HashMap<>(); void addEdge(int u, int v, int w) { adjList.get(u).add(new int[]{v, w}); } void printGraph() { for (int key : adjList.keySet()) { System.out.print(key + " -> "); for (int[] pair : adjList.get(key)) { System.out.print("(" + pair[0] + ", " + pair[1] + ") "); } System.out.println("null"); } } } public class Main { public static void main(String[] args) { 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 if the adjacency list for a vertex is initialized before adding edges.
✗ Incorrect
The code tries to add to a list for a vertex key that was never initialized, causing a NullPointerException.
