Challenge - 5 Problems
Graph Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of adjacency list for directed weighted graph
What is the printed adjacency list after running this TypeScript code for a directed weighted graph?
DSA Typescript
type Edge = { to: number; weight: number }; const graph: Map<number, Edge[]> = new Map(); function addEdge(from: number, to: number, weight: number) { if (!graph.has(from)) graph.set(from, []); graph.get(from)!.push({ to, weight }); } addEdge(1, 2, 5); addEdge(1, 3, 10); addEdge(2, 3, 2); addEdge(3, 1, 7); for (const [node, edges] of graph.entries()) { console.log(`${node} -> ${edges.map(e => `${e.to}(${e.weight})`).join(', ')}`); }
Attempts:
2 left
💡 Hint
Check how edges are added and printed for each node in the directed graph.
✗ Incorrect
The code adds edges from one node to another with weights. It prints each node's edges exactly as added. Option B matches the exact output.
🧠 Conceptual
intermediate1:30remaining
Identify the type of graph from description
Which type of graph is described by: "Edges have no direction and each edge has a number representing cost or distance"?
Attempts:
2 left
💡 Hint
Edges without direction mean connections go both ways.
✗ Incorrect
Edges without direction are undirected. Having a number for cost means weighted. So it is an undirected weighted graph.
🔧 Debug
advanced2:00remaining
Find the error in this undirected graph edge addition
What error will occur when running this TypeScript code to add edges in an undirected graph?
DSA Typescript
const graph: Map<number, number[]> = new Map();
function addEdge(u: number, v: number) {
if (!graph.has(u)) graph.set(u, []);
graph.get(u)!.push(v);
graph.get(v)!.push(u);
}
addEdge(1, 2);
addEdge(2, 3);
console.log(graph);Attempts:
2 left
💡 Hint
Check if both nodes have arrays initialized before pushing.
✗ Incorrect
When adding edge from v to u, graph.get(v) may be undefined if v was not initialized, causing a TypeError.
❓ Predict Output
advanced2:00remaining
Output of BFS traversal on undirected graph
What is the order of nodes printed by this BFS traversal on the undirected graph?
DSA Typescript
const graph: Map<number, number[]> = new Map();
function addEdge(u: number, v: number) {
if (!graph.has(u)) graph.set(u, []);
if (!graph.has(v)) graph.set(v, []);
graph.get(u)!.push(v);
graph.get(v)!.push(u);
}
addEdge(1, 2);
addEdge(1, 3);
addEdge(2, 4);
addEdge(3, 5);
function bfs(start: number) {
const visited = new Set<number>();
const queue: number[] = [];
queue.push(start);
visited.add(start);
const order: number[] = [];
while (queue.length > 0) {
const node = queue.shift()!;
order.push(node);
for (const neighbor of graph.get(node)!) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
return order;
}
console.log(bfs(1).join(' -> '));Attempts:
2 left
💡 Hint
BFS visits neighbors level by level in the order they appear in adjacency list.
✗ Incorrect
BFS starts at 1, visits neighbors 2 and 3 in order, then visits neighbors of 2 (4), then neighbors of 3 (5).
🧠 Conceptual
expert1:30remaining
Which graph type allows cycles and weighted edges but no direction?
Select the graph type that can have cycles, edges with weights, and edges without direction.
Attempts:
2 left
💡 Hint
Edges without direction and with weights allow cycles.
✗ Incorrect
Undirected weighted graphs can have cycles and weighted edges without direction.