Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Adjacency List vs Matrix When to Choose Which in DSA Typescript - Compare & Choose

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Adjacency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When is an adjacency list better than an adjacency matrix?
Choose the best scenario where using an adjacency list is more efficient than an adjacency matrix.
AWhen the graph is dense with many edges close to the maximum possible.
BWhen you need to quickly check if an edge exists between any two nodes.
CWhen the graph is sparse with relatively few edges compared to the number of nodes.
DWhen the graph has a fixed small number of nodes and many edges.
Attempts:
2 left
💡 Hint

Think about memory usage and how many edges are stored.

❓ Predict Output
intermediate
2:00remaining
Output of adjacency matrix edge check
What is the output of the following TypeScript code that uses an adjacency matrix to check for an edge?
DSA Typescript
const graph = [
  [0, 1, 0],
  [1, 0, 1],
  [0, 1, 0]
];

console.log(graph[0][2]);
A1
BTypeError
Cundefined
D0
Attempts:
2 left
💡 Hint

Look at the value stored at row 0, column 2.

❓ Predict Output
advanced
2:00remaining
Output of adjacency list traversal
What will be printed by this TypeScript code that uses an adjacency list to print neighbors of node 1?
DSA Typescript
const graph: number[][] = [
  [1],       // neighbors of node 0
  [0, 2],    // neighbors of node 1
  [1]        // neighbors of node 2
];

for (const neighbor of graph[1]) {
  console.log(neighbor);
}
A0\n2
B1\n2
C1
D0
Attempts:
2 left
💡 Hint

Look at the neighbors stored for node 1.

🧠 Conceptual
advanced
2:00remaining
Why is adjacency matrix better for dense graphs?
Select the main reason adjacency matrix is preferred over adjacency list for dense graphs.
ABecause adjacency matrix is easier to implement for sparse graphs.
BBecause adjacency matrix allows faster edge existence checks between any two nodes.
CBecause adjacency matrix uses less memory for dense graphs.
DBecause adjacency matrix stores neighbors as lists.
Attempts:
2 left
💡 Hint

Think about how quickly you can check if an edge exists.

🚀 Application
expert
3:00remaining
Choosing data structure for a social network graph
You are designing a social network with millions of users but each user has only a few friends on average. Which data structure is best to represent the friendship connections and why?
AUse an adjacency list because it saves memory by storing only existing friendships.
BUse a 2D array with all zeros because most users have no friends.
CUse an adjacency matrix because it allows quick checks if two users are friends.
DUse a linked list for each user storing all users in the network.
Attempts:
2 left
💡 Hint

Consider memory usage and average number of connections per user.

Practice

(1/5)
1. Which data structure is generally more space-efficient for representing a graph with very few edges compared to the number of vertices?
easy
A. Adjacency List
B. Adjacency Matrix
C. Edge List
D. Incidence Matrix

Solution

  1. Step 1: Understand graph density

    Sparse graphs have few edges compared to vertices, so storing all possible edges wastes space.
  2. Step 2: Compare space usage of adjacency list and matrix

    Adjacency list stores only existing edges, using less space for sparse graphs. Adjacency matrix always uses space for all possible edges.
  3. Final Answer:

    Adjacency List -> Option A
  4. Quick Check:

    Sparse graph = Adjacency List [OK]
Hint: Sparse graph? Use adjacency list for less space [OK]
Common Mistakes:
  • Choosing adjacency matrix for sparse graphs
  • Confusing edge count with vertex count
  • Ignoring space complexity differences
2. Which of the following is the correct TypeScript syntax to declare an adjacency matrix for a graph with 3 vertices initialized with zeros?
easy
A. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0));
B. const matrix: number[] = [0,0,0];
C. const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]];
D. const matrix: number[][] = Array(3).map(() => Array(3).fill(0));

Solution

  1. Step 1: Understand adjacency matrix type

    An adjacency matrix is a 2D array (array of arrays) of numbers representing edges.
  2. Step 2: Check initialization correctness

    const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; explicitly creates a 3x3 matrix with zeros. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0)); uses fill with the same inner array reference, causing bugs. const matrix: number[][] = Array(3).map(() => Array(3).fill(0)); uses map on empty array, which won't work as expected. const matrix: number[] = [0,0,0]; is 1D array, not matrix.
  3. Final Answer:

    const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; -> Option C
  4. Quick Check:

    Explicit 2D array with zeros = const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; [OK]
Hint: Use explicit 2D array for adjacency matrix initialization [OK]
Common Mistakes:
  • Using fill with nested arrays causing shared references
  • Declaring 1D array instead of 2D
  • Using map on empty array without initialization
3. Given the following adjacency list representation of a graph in TypeScript:
const graph = {
  0: [1, 2],
  1: [2],
  2: [0, 3],
  3: []
};
console.log(graph[2]);

What will be the output?
medium
A. [2]
B. [1, 2]
C. undefined
D. [0, 3]

Solution

  1. Step 1: Identify the adjacency list for vertex 2

    In the graph object, key '2' maps to the array [0, 3].
  2. Step 2: Understand console.log output

    Logging graph[2] prints the array [0, 3] representing edges from vertex 2.
  3. Final Answer:

    [0, 3] -> Option D
  4. Quick Check:

    graph[2] = [0, 3] [OK]
Hint: Access adjacency list by vertex key to get connected nodes [OK]
Common Mistakes:
  • Confusing keys and values in adjacency list
  • Expecting adjacency matrix output
  • Assuming undefined for existing keys
4. Consider this TypeScript code snippet for creating an adjacency matrix for a graph with 3 vertices:
const size = 3;
const matrix = new Array(size).fill(new Array(size).fill(0));
matrix[0][1] = 1;
console.log(matrix);

What is the problem with this code?
medium
A. All rows refer to the same inner array, so all rows change when one element changes
B. The matrix is not initialized with zeros
C. The matrix is not a 2D array
D. The size variable is not used correctly

Solution

  1. Step 1: Analyze the fill method usage

    Using fill with new Array(size).fill(0) creates one inner array and fills outer array with references to the same inner array.
  2. Step 2: Understand effect of modifying one element

    Changing matrix[0][1] changes that element in all rows because they share the same inner array reference.
  3. Final Answer:

    All rows refer to the same inner array, so all rows change when one element changes -> Option A
  4. Quick Check:

    Shared inner array reference causes unexpected changes [OK]
Hint: Avoid fill with nested arrays; use map to create separate arrays [OK]
Common Mistakes:
  • Thinking fill creates separate inner arrays
  • Ignoring shared reference problem
  • Assuming matrix is 1D array
5. You need to represent a dense graph with 1000 vertices where you frequently check if an edge exists between any two vertices. Which representation is best and why?
hard
A. Adjacency List, because it uses less space for dense graphs
B. Adjacency Matrix, because it allows O(1) edge existence checks
C. Edge List, because it is fastest for dense graphs
D. Incidence Matrix, because it uses less space than adjacency matrix

Solution

  1. Step 1: Understand graph density and operation needs

    Dense graph means many edges; checking edge existence frequently requires fast lookup.
  2. Step 2: Compare adjacency list and matrix for edge checks

    Adjacency matrix allows direct O(1) access to check if edge exists between two vertices. Adjacency list requires searching neighbors, which can be slower in dense graphs.
  3. Step 3: Consider space tradeoff

    Though adjacency matrix uses more space, for dense graphs this is acceptable and beneficial for speed.
  4. Final Answer:

    Adjacency Matrix, because it allows O(1) edge existence checks -> Option B
  5. Quick Check:

    Dense graph + frequent edge check = Adjacency Matrix [OK]
Hint: Dense graph + frequent edge check? Use adjacency matrix [OK]
Common Mistakes:
  • Choosing adjacency list for dense graphs needing fast edge checks
  • Ignoring time complexity of edge existence queries
  • Confusing edge list with adjacency matrix benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard