0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 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.