0
0
DSA Typescriptprogramming~20 mins

Adjacency List Representation in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Adjacency List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adjacency list after adding edges
What is the printed adjacency list after executing the following TypeScript code?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();

  addVertex(v: number) {
    if (!this.adjacencyList.has(v)) {
      this.adjacencyList.set(v, []);
    }
  }

  addEdge(v: number, w: number) {
    this.adjacencyList.get(v)?.push(w);
    this.adjacencyList.get(w)?.push(v);
  }

  printGraph() {
    this.adjacencyList.forEach((neighbors, vertex) => {
      console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`);
    });
  }
}

const g = new Graph();
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.printGraph();
A
1 -&gt; 3 -&gt; 2 -&gt; null
2 -&gt; 1 -&gt; null
3 -&gt; 1 -&gt; null
B
1 -&gt; 2 -&gt; 3 -&gt; null
2 -&gt; 3 -&gt; 1 -&gt; null
3 -&gt; 2 -&gt; 1 -&gt; null
C
1 -&gt; 2 -&gt; null
2 -&gt; 1 -&gt; 3 -&gt; null
3 -&gt; 1 -&gt; null
D
1 -&gt; 2 -&gt; 3 -&gt; null
2 -&gt; 1 -&gt; null
3 -&gt; 1 -&gt; null
Attempts:
2 left
💡 Hint
Remember edges are added in both directions for an undirected graph.
Predict Output
intermediate
2:00remaining
Adjacency list after removing a vertex
What is the adjacency list printed after removing vertex 2 from the graph below?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();

  addVertex(v: number) {
    if (!this.adjacencyList.has(v)) {
      this.adjacencyList.set(v, []);
    }
  }

  addEdge(v: number, w: number) {
    this.adjacencyList.get(v)?.push(w);
    this.adjacencyList.get(w)?.push(v);
  }

  removeVertex(v: number) {
    this.adjacencyList.delete(v);
    this.adjacencyList.forEach((neighbors) => {
      const index = neighbors.indexOf(v);
      if (index !== -1) {
        neighbors.splice(index, 1);
      }
    });
  }

  printGraph() {
    this.adjacencyList.forEach((neighbors, vertex) => {
      console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`);
    });
  }
}

const g = new Graph();
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(1, 3);
g.removeVertex(2);
g.printGraph();
A
1 -&gt; 3 -&gt; null
3 -&gt; 1 -&gt; null
B
1 -&gt; 2 -&gt; 3 -&gt; null
3 -&gt; 1 -&gt; 2 -&gt; null
C
1 -&gt; null
3 -&gt; null
D
1 -&gt; 3 -&gt; 2 -&gt; null
3 -&gt; 1 -&gt; null
Attempts:
2 left
💡 Hint
Removing a vertex also removes it from neighbors' lists.
🔧 Debug
advanced
2:00remaining
Identify the error in adjacency list edge addition
What error will the following TypeScript code cause when adding an edge?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();

  addVertex(v: number) {
    if (!this.adjacencyList.has(v)) {
      this.adjacencyList.set(v, []);
    }
  }

  addEdge(v: number, w: number) {
    this.adjacencyList.get(v).push(w);
    this.adjacencyList.get(w).push(v);
  }
}

const g = new Graph();
g.addVertex(1);
g.addEdge(1, 2);
ASyntaxError: Unexpected token '.'
BNo error, edge added successfully
CTypeError: Cannot read property 'push' of undefined
DReferenceError: g is not defined
Attempts:
2 left
💡 Hint
Check if vertices exist before adding edges.
🧠 Conceptual
advanced
2:00remaining
Memory efficiency of adjacency list vs adjacency matrix
Which statement correctly explains why adjacency lists are more memory efficient than adjacency matrices for sparse graphs?
AAdjacency lists store only existing edges, while adjacency matrices allocate space for all possible edges.
BAdjacency matrices store edges as linked lists, which use more memory than arrays.
CAdjacency lists require a fixed-size 2D array, increasing memory usage.
DAdjacency matrices compress edges, making them less memory efficient.
Attempts:
2 left
💡 Hint
Think about how each structure stores edges.
🚀 Application
expert
3:00remaining
Resulting adjacency list after multiple operations
After running the following TypeScript code, what is the printed adjacency list?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();

  addVertex(v: number) {
    if (!this.adjacencyList.has(v)) {
      this.adjacencyList.set(v, []);
    }
  }

  addEdge(v: number, w: number) {
    this.adjacencyList.get(v)?.push(w);
    this.adjacencyList.get(w)?.push(v);
  }

  removeEdge(v: number, w: number) {
    this.adjacencyList.set(v, this.adjacencyList.get(v)?.filter(x => x !== w) || []);
    this.adjacencyList.set(w, this.adjacencyList.get(w)?.filter(x => x !== v) || []);
  }

  printGraph() {
    this.adjacencyList.forEach((neighbors, vertex) => {
      console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`);
    });
  }
}

const g = new Graph();
g.addVertex(1);
g.addVertex(2);
g.addVertex(3);
g.addVertex(4);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
g.addEdge(3, 4);
g.removeEdge(1, 3);
g.printGraph();
A
1 -&gt; 2 -&gt; 3 -&gt; null
2 -&gt; 1 -&gt; 4 -&gt; null
3 -&gt; 4 -&gt; 1 -&gt; null
4 -&gt; 2 -&gt; 3 -&gt; null
B
1 -&gt; 2 -&gt; null
2 -&gt; 1 -&gt; 4 -&gt; null
3 -&gt; 4 -&gt; null
4 -&gt; 2 -&gt; 3 -&gt; null
C
1 -&gt; 3 -&gt; null
2 -&gt; 4 -&gt; null
3 -&gt; 1 -&gt; 4 -&gt; null
4 -&gt; 2 -&gt; 3 -&gt; null
D
1 -&gt; 2 -&gt; null
2 -&gt; 4 -&gt; null
3 -&gt; 4 -&gt; null
4 -&gt; 2 -&gt; 3 -&gt; null
Attempts:
2 left
💡 Hint
Removing edge (1,3) removes 3 from 1's list and 1 from 3's list.