Checkpoint
Graphs
Twelve items from all six lessons: grids and adjacency lists, depth-first and breadth-first search, topological order, union-find and Dijkstra. Every input is new, so work each one on paper: write down the stack, the queue, the in-degrees, the parent list or the heap after every step, because that is where the classic mistakes show. The last two items are code; the rest take a few minutes each.
- Question 1 of 12.
This loop counts the land neighbors (up, down, left and right) of the top-left cell, (0, 0), on a 3 by 3 map where 1 is land. Its bounds check tests only the upper limits. Type exactly what it prints.
grid = [[1, 0, 1], [1, 1, 0], [0, 1, 1]] rows, cols = 3, 3 r, c = 0, 0 count = 0 for dr, dc in [(1, 0), (-1, 0), (0, 1), (0, -1)]: nr, nc = r + dr, c + dc if nr < rows and nc < cols and grid[nr][nc] == 1: count += 1 print(count) - Question 2 of 12.
A two-way graph has
n = 8nodes, numbered 0 to 7, and the edges[[0, 1], [2, 3], [1, 3], [4, 5], [6, 6]]. The last edge joins node 6 to itself. How many connected components (groups of nodes that can all reach each other) does it have? Type a whole number.componentsType a number: 12, -1, 0.5 and 3/4 all work.
- Question 3 of 12.
Your recursive island search (one call per land cell, and each call goes deeper before it returns) passes every small test. The interviewer asks about a 300 by 300 map that is all land, run in plain Python. What happens?
- Question 4 of 12.
This breadth-first search marks a node visited when it leaves the queue, not when it joins. How many times does G leave the queue? Type a whole number.
from collections import deque graph = {"A": ["B", "C"], "B": ["D"], "C": ["D"], "D": ["E", "F"], "E": ["G"], "F": ["G"], "G": []} queue = deque(["A"]) visited = set() while queue: node = queue.popleft() visited.add(node) # marked when it leaves for neighbor in graph[node]: if neighbor not in visited: queue.append(neighbor)timesType a number: 12, -1, 0.5 and 3/4 all work.
- Question 5 of 12.
A grid of numbers from 0 to 9: stepping onto a cell costs its number. You move up, down, left or right. Which method finds the cheapest total cost from the top-left cell to the bottom-right cell?
- Question 6 of 12.
Six tasks, with each arrow written
[before, after]. Type exactly what this prints.from collections import deque arrows = [[5, 2], [5, 0], [4, 0], [4, 1], [2, 3], [3, 1]] dependents = [[] for _ in range(6)] in_degree = [0] * 6 for before, after in arrows: dependents[before].append(after) in_degree[after] += 1 ready = deque(t for t in range(6) if in_degree[t] == 0) order = [] while ready: task = ready.popleft() order.append(task) for nxt in dependents[task]: in_degree[nxt] -= 1 if in_degree[nxt] == 0: ready.append(nxt) print(order) - Question 7 of 12.
A Course Schedule input:
num_courses = 4,prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]], where[a, b]means coursebmust be finished before coursea. A classmate's checker walks from course 0 and reports a cycle, because it reaches course 3 twice: once through 1 and once through 2. What does Kahn's algorithm say? - Question 8 of 12.
This union-find compares roots correctly, but then links the item
binstead of its root. Type exactly what it prints.parent = list(range(4)) groups = 4 def find(x): while parent[x] != x: x = parent[x] return x for a, b in [(0, 1), (2, 1), (3, 0)]: if find(a) != find(b): parent[b] = a # should be parent[find(b)] = find(a) groups -= 1 print(groups, find(0) == find(2)) - Question 9 of 12.
Eight items, 0 to 7, start as their own roots. Run these unions with union by size, where on a tie the second root goes under the first, as in the lesson:
(0, 1), (2, 3), (0, 2), (4, 5), (6, 7), (4, 6), (0, 4)
Every pair names two roots, so no find in these unions climbs, and no path gets compressed. How many steps up does
find(7)climb at the end? Type a whole number.stepsType a number: 12, -1, 0.5 and 3/4 all work.
- Question 10 of 12.
Run Dijkstra from S on these one-way links, checking each node's links in the order listed. How many pops are stale, skipped because the node already has a smaller distance? Type a whole number.
link weight S to T 10 S to A 1 A to T 7 A to B 1 B to T 2 stale popsType a number: 12, -1, 0.5 and 3/4 all work.
- Question 11 of 12.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itFewest knight jumps A knight on an n by n chessboard jumps in an L: two squares one way and one square at a right angle. From (r, c) it can land on (r + 1, c + 2), (r + 2, c + 1), and the six other squares you get by flipping those signs, as long as the square is on the board.
Write
knight_moves(n, start, target), which returns the fewest jumps fromstarttotarget(each a(row, col)pair), or -1 if the knight can never get there. For example,knight_moves(8, (0, 0), (7, 7))is 6.Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects. - Question 12 of 12.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itCables to move A server room has
nservers, numbered 0 to n − 1, and a listcables, where each[a, b]joins two servers both ways. A server can reach another through any chain of cables. You may unplug any cable and plug it in between any two servers.Write
cables_to_move(n, cables), which returns the fewest cables you must move so that every server can reach every other, or -1 if there are not enough cables. For example, with 5 servers and[[0, 1], [1, 2], [0, 2], [3, 4]]the answer is 1: 0 and 2 are already joined through 1, so the cable [0, 2] can move to join, say, 2 and 3. This is LeetCode 1319, Number of Operations to Make Network Connected.Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects.