diff --git a/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json b/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json index b1567fd256..665e37562d 100644 --- a/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json +++ b/challenges/08-coding-interview-questions-and-take-home-assignments/coding-interview-data-structure-questions.json @@ -2821,18 +2821,18 @@ "id": "587d825c367417b2b2512c90", "title": "Breadth-First Search", "description": [ - "So far, we've learned different ways of creating representations of graphs. What now? One natural question to have is what are the distances between any two nodes in the graph? Enter graph traversal algorithms.", - "Traversal algorithms are algorithms to traverse or visit nodes in a graph. One type of traversal algorithm is the breadth-first search algorithm.", + "So far, we've learned different ways of creating representations of graphs. What now? One natural question to have is what are the distances between any two nodes in the graph? Enter graph traversal algorithms.", + "Traversal algorithms are algorithms to traverse or visit nodes in a graph. One type of traversal algorithm is the breadth-first search algorithm.", "This algorithm starts at one node, first visits all its neighbors that are one edge away, then goes on to visiting each of their neighbors.", "Visually, this is what the algorithm is doing.", - "", + "", "To implement this algorithm, you'll need to input a graph structure and a node you want to start at.", - "First, you'll want to be aware of the distances from the start node. This you'll want to start all your distances initially some large number, like Infinity. This gives a reference for the case where a node may not be reachable from your start node.", + "First, you'll want to be aware of the distances from the start node. This you'll want to start all your distances initially some large number, like Infinity. This gives a reference for the case where a node may not be reachable from your start node.", "Next, you'll want to go from the start node to its neighbors. These neighbors are one edge away and at this point you should add one unit of distance to the distances you're keeping track of.", - "Last, an important data structure that will help implement the breadth-first search algorithm is the queue. This is an array where you can add elements to one end and remove elements from the other end. This is also known as a FIFO or First-In-First-Out data structure.", - "Instructions", - "Write a function bfs() that takes an adjacency matrix graph (a two-dimensional array) and a node label root as parameters. The node label will just be the integer value of the node between 0 and n - 1, where n is the total number of nodes in the graph.", - "Your function will output a JavaScript object key-value pairs with the node and its distance from the root. If the node could not be reached, it should have a distance of Infinity." + "Last, an important data structure that will help implement the breadth-first search algorithm is the queue. This is an array where you can add elements to one end and remove elements from the other end. This is also known as a FIFO or First-In-First-Out data structure.", + "
", + "Write a function bfs() that takes an adjacency matrix graph (a two-dimensional array) and a node label root as parameters. The node label will just be the integer value of the node between 0 and n - 1, where n is the total number of nodes in the graph.", + "Your function will output a JavaScript object key-value pairs with the node and its distance from the root. If the node could not be reached, it should have a distance of Infinity." ], "challengeSeed": [ "function bfs(graph, root) {", @@ -2840,7 +2840,8 @@ " var nodesLen = {};", " ", " return nodesLen;", - "};", + "}", + "", "var exBFSGraph = [", " [0, 1, 0, 0],", " [1, 0, 1, 0],", @@ -2851,7 +2852,7 @@ ], "tests": [ "assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]; var results = bfs(graph, 1); return isEquivalent(results, {0: 1, 1: 0, 2: 1, 3: 2})})(), 'message: The input graph [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]] with a start node of 1 should return {0: 1, 1: 0, 2: 1, 3: 2}');", - "assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]; var results = bfs(graph, 1); return isEquivalent(results, {0: 1, 1: 0, 2: 1, 3: 2})})(), 'message: The input graph [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]] with a start node of 1 should return {0: 1, 1: 0, 2: 1, 3: Infinity}');", + "assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]; var results = bfs(graph, 1); return isEquivalent(results, {0: 1, 1: 0, 2: 1, 3: Infinity})})(), 'message: The input graph [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]] with a start node of 1 should return {0: 1, 1: 0, 2: 1, 3: Infinity}');", "assert((function() { var graph = [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]; var results = bfs(graph, 0); return isEquivalent(results, {0: 0, 1: 1, 2: 2, 3: 3})})(), 'message: The input graph [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]] with a start node of 0 should return {0: 0, 1: 1, 2: 2, 3: 3}');", "assert((function() { var graph = [[0, 1], [1, 0]]; var results = bfs(graph, 0); return isEquivalent(results, {0: 0, 1: 1})})(), 'message: The input graph [[0, 1], [1, 0]] with a start node of 0 should return {0: 0, 1: 1}');" ], @@ -2880,7 +2881,9 @@ "}" ], "type": "waypoint", - "solutions": [], + "solutions": [ + "function bfs(graph, root) {\n// Distance object returned\nvar nodesLen = {};\n// Set all distances to infinity\nfor (var i = 0; i < graph.length; i++) {\nnodesLen[i] = Infinity;\n}\nnodesLen[root] = 0; // ...except root node\nvar queue = [root]; // Keep track of nodes to visit\nvar current; // Current node traversing\n// Keep on going until no more nodes to traverse\nwhile (queue.length !== 0) {\ncurrent = queue.shift();\n// Get adjacent nodes from current node\nvar curConnected = graph[current]; // Get layer of edges from current\nvar neighborIdx = []; // List of nodes with edges\nvar idx = curConnected.indexOf(1); // Get first edge connection\nwhile (idx !== -1) {\nneighborIdx.push(idx); // Add to list of neighbors\nidx = curConnected.indexOf(1, idx + 1); // Keep on searching\n}\n// Loop through neighbors and get lengths\nfor (var j = 0; j < neighborIdx.length; j++) {\n// Increment distance for nodes traversed\nif (nodesLen[neighborIdx[j]] === Infinity) {\nnodesLen[neighborIdx[j]] = nodesLen[current] + 1;\nqueue.push(neighborIdx[j]); // Add new neighbors to queue\n}\n}\n}\nreturn nodesLen;}" + ], "challengeType": 1, "translations": {} },