📖 Scenario: Imagine you are organizing a small social network where each person can be friends with others. You want to store who is friends with whom in a way that is easy to look up and update.
🎯 Goal: You will build an adjacency list representation of a graph using a TypeScript object. Each key will be a person's name, and the value will be a list of their friends' names.
📋 What You'll Learn
Create an adjacency list object with exact keys and values
Add a new person with an empty friends list
Add friends to a person's list
Print the adjacency list to show the connections
💡 Why This Matters
🌍 Real World
Adjacency lists are used to represent networks like social connections, road maps, or computer networks efficiently.
💼 Career
Understanding adjacency lists is important for roles in software development, data analysis, and network engineering where graph data structures are common.
Progress0 / 4 steps
1
Create the initial adjacency list
Create a variable called adjacencyList as an object with these exact entries: 'Alice': ['Bob', 'Charlie'], 'Bob': ['Alice'], 'Charlie': ['Alice']
DSA Typescript
Hint
Use a TypeScript object with string keys and array of strings as values.
2
Add a new person with no friends
Add a new key 'David' to adjacencyList with an empty array as value
DSA Typescript
Hint
Use bracket notation to add a new key with an empty array.
3
Add friends to David's list
Add 'Alice' and 'Bob' to adjacencyList['David'] array using push method
DSA Typescript
Hint
Use push method twice to add two friends.
4
Print the adjacency list
Print the adjacencyList variable using console.log
DSA Typescript
Hint
Use console.log(adjacencyList) to print the whole object.
Practice
(1/5)
1. What does an adjacency list represent in graph data structures?
easy
A. A list of all nodes without connections
B. A matrix showing distances between nodes
C. A queue of nodes to visit next
D. A list where each node stores its connected neighbors
Solution
Step 1: Understand adjacency list concept
An adjacency list stores each node with a list of its neighbors, showing direct connections.
Step 2: Compare options with definition
Only A list where each node stores its connected neighbors correctly describes this structure; others describe different concepts.
Final Answer:
A list where each node stores its connected neighbors -> Option D
Quick Check:
Adjacency list = neighbors list [OK]
Hint: Adjacency list always stores neighbors per node [OK]
Common Mistakes:
Confusing adjacency list with adjacency matrix
Thinking it stores distances instead of neighbors
Assuming it is a queue or unrelated structure
2. Which of the following is the correct TypeScript syntax to declare an adjacency list for a graph with nodes as numbers?
easy
A. const graph: number[][] = [];
B. const graph: Map = new Map();
C. const graph: Record = {};
D. const graph: Array<number> = [];
Solution
Step 1: Identify adjacency list type
An adjacency list maps each node (number) to an array of connected nodes (number[]).
Step 2: Match TypeScript syntax
Map correctly represents this; others are incorrect types or structures.
Final Answer:
const graph: Map = new Map(); -> Option B
Quick Check:
Map = adjacency list [OK]
Hint: Use Map with number keys and array values for adjacency list [OK]
Common Mistakes:
Using Record which maps to single number, not array
Using number[][] without keys for nodes
Using Array<number> which is just a list, not adjacency
3. Given the following TypeScript code, what is the output after adding edges?
Thinking Map keys must be strings (numbers are allowed)
Ignoring runtime error from push on undefined
5. You want to represent a directed graph with nodes 1 to 4 using an adjacency list in TypeScript. Which code correctly adds an edge from node 2 to node 4 without affecting other edges?
const graph: Map = new Map();
graph.set(1, [2]);
graph.set(2, [3]);
graph.set(3, []);
graph.set(4, []);
// Add edge from 2 to 4 here
hard
A. graph.get(2)?.push(4);
B. graph.set(2, [4]);
C. graph.set(4, [2]);
D. graph.get(4)?.push(2);
Solution
Step 1: Understand existing edges
Node 2 currently has neighbors [3]. We want to add 4 without removing 3.
Step 2: Choose correct method to add edge
Using graph.get(2)?.push(4) adds 4 to existing array, preserving [3]. graph.set(2, [4]) replaces neighbors, removing 3.
Final Answer:
graph.get(2)?.push(4); -> Option A
Quick Check:
Use push to add edge without overwriting [OK]
Hint: Use push on existing neighbors to add edges safely [OK]
Common Mistakes:
Overwriting neighbors array with set instead of pushing
Adding edge to wrong node
Confusing direction of edge in directed graph
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.