Challenge - 5 Problems
Graph vs Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Key Structural Difference Between Graph and Tree
Which of the following statements correctly describes the key structural difference between a graph and a tree?
Attempts:
2 left
💡 Hint
Think about connectivity and cycles in both structures.
✗ Incorrect
A tree is a special type of graph that is connected and has no cycles. Graphs are more general and can have cycles and may be disconnected.
❓ Predict Output
intermediate1:30remaining
Output of Graph vs Tree Edge Count
Given a tree with 5 nodes, what is the number of edges it must have?
DSA Typescript
const nodes = 5; const edgesInTree = nodes - 1; console.log(edgesInTree);
Attempts:
2 left
💡 Hint
Remember the formula for edges in a tree.
✗ Incorrect
A tree with n nodes always has n-1 edges.
❓ Predict Output
advanced2:30remaining
Detecting Cycle in Graph Code Output
What is the output of the following TypeScript code that checks for a cycle in an undirected graph?
DSA Typescript
class Graph { adjacencyList: Map<number, number[]> = new Map(); addEdge(u: number, v: number) { if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []); if (!this.adjacencyList.has(v)) this.adjacencyList.set(v, []); this.adjacencyList.get(u)!.push(v); this.adjacencyList.get(v)!.push(u); } hasCycle(): boolean { const visited = new Set<number>(); const dfs = (node: number, parent: number): boolean => { visited.add(node); for (const neighbor of this.adjacencyList.get(node) || []) { if (!visited.has(neighbor)) { if (dfs(neighbor, node)) return true; } else if (neighbor !== parent) { return true; } } return false; }; for (const node of this.adjacencyList.keys()) { if (!visited.has(node)) { if (dfs(node, -1)) return true; } } return false; } } const g = new Graph(); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(3, 4); g.addEdge(4, 1); console.log(g.hasCycle());
Attempts:
2 left
💡 Hint
Check if the edges form a cycle.
✗ Incorrect
The edges form a cycle: 1-2-3-4-1, so the function returns true.
🧠 Conceptual
advanced2:00remaining
Why Trees Cannot Have Cycles
Why is it impossible for a tree to have a cycle?
Attempts:
2 left
💡 Hint
Think about the uniqueness of paths in a tree.
✗ Incorrect
A tree has exactly one path between any two nodes, so cycles are not possible.
❓ Predict Output
expert2:30remaining
Output of Graph Connectivity Check
What is the output of this TypeScript code that checks if an undirected graph is connected?
DSA Typescript
class Graph { adjacencyList: Map<number, number[]> = new Map(); addEdge(u: number, v: number) { if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []); if (!this.adjacencyList.has(v)) this.adjacencyList.set(v, []); this.adjacencyList.get(u)!.push(v); this.adjacencyList.get(v)!.push(u); } isConnected(): boolean { const visited = new Set<number>(); const nodes = Array.from(this.adjacencyList.keys()); if (nodes.length === 0) return true; const dfs = (node: number) => { visited.add(node); for (const neighbor of this.adjacencyList.get(node) || []) { if (!visited.has(neighbor)) dfs(neighbor); } }; dfs(nodes[0]); return visited.size === nodes.length; } } const g = new Graph(); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(4, 5); console.log(g.isConnected());
Attempts:
2 left
💡 Hint
Check if all nodes are reachable from the first node.
✗ Incorrect
The graph has two disconnected parts: 1-2-3 and 4-5, so it is not connected.