Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a graph node with a list of neighbors.
DSA Typescript
class GraphNode { value: number; neighbors: GraphNode[]; constructor(value: number) { this.value = value; this.neighbors = [1]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which creates an object instead of an array.
Setting neighbors to null or 0 which are not lists.
✗ Incorrect
The neighbors list should be initialized as an empty array to hold connected nodes.
2fill in blank
mediumComplete the code to add an edge between two graph nodes.
DSA Typescript
function addEdge(node1: GraphNode, node2: GraphNode) {
node1.neighbors.[1](node2);
node2.neighbors.push(node1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append which is not a method on arrays in TypeScript.
Using add or insert which are not valid array methods.
✗ Incorrect
The push method adds an element to the end of an array in TypeScript.
3fill in blank
hardFix the error in the code to check if a graph has a cycle.
DSA Typescript
function hasCycle(node: GraphNode, visited: Set<number>): boolean {
if (visited.has(node.value)) {
return [1];
}
visited.add(node.value);
for (const neighbor of node.neighbors) {
if (hasCycle(neighbor, visited)) {
return true;
}
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false which means no cycle found incorrectly.
Returning null or undefined which are not boolean values.
✗ Incorrect
If the node was already visited, it means a cycle exists, so return true.
4fill in blank
hardFill both blanks to create an adjacency list representation of a graph.
DSA Typescript
const graph: [1] = {}; function addEdge(u: number, v: number) { if (!graph[u]) graph[u] = [2]; graph[u].push(v); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} for adjacency lists which is an object, not an array.
Using Array as the graph type which is incorrect.
✗ Incorrect
The graph is a record mapping numbers to arrays of numbers. Initialize empty arrays for adjacency lists.
5fill in blank
hardFill all three blanks to implement a breadth-first search (BFS) on a graph.
DSA Typescript
function bfs(start: number, graph: Record<number, number[]>): number[] {
const visited = new Set<number>();
const queue: number[] = [start];
const result: number[] = [];
while (queue.length > 0) {
const node = queue.[1]();
if (!visited.has(node)) {
visited.add(node);
result.push(node);
for (const neighbor of graph[node] || []) {
if (!visited.has(neighbor)) {
queue.[2](neighbor);
}
}
}
}
return [3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes from the end, causing DFS behavior.
Returning queue instead of the result list.
✗ Incorrect
Use shift() to dequeue from the front, push() to enqueue neighbors, and return the result list.