0
0
DSA Typescriptprogramming~10 mins

Why Graphs Exist and What Trees Cannot Model in DSA Typescript - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A[]
B{}
Cnull
D0
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.
2fill in blank
medium

Complete 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'
Aappend
Binsert
Cadd
Dpush
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.
3fill in blank
hard

Fix 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'
Afalse
Btrue
Cnull
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false which means no cycle found incorrectly.
Returning null or undefined which are not boolean values.
4fill in blank
hard

Fill 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'
ARecord<number, number[]>
BArray<number>
C[]
D{}
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.
5fill in blank
hard

Fill 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'
Ashift
Bpush
Cresult
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes from the end, causing DFS behavior.
Returning queue instead of the result list.