Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many paths of length exactly 4 go from vertex 0 to vertex 0 in the complete graph K3? Run the code (walks allowed to repeat vertices).
Problem: How many paths of length exactly 4 go from vertex 0 to vertex 0 in the complete graph K3? Run the code (walks allowed to repeat vertices).
n = 3
adj = [[1 if i != j else 0 for j in range(n)] for i in range(n)]
P = [[int(i == j) for j in range(n)] for i in range(n)]
for _ in range(4):
P = [[sum(P[i][k] * adj[k][j] for k in range(n)) for j in range(n)] for i in range(n)]
print(P[0][0])What is printed?- A. 5
- B. 18
- C. 16
- D. 6
Correct answer: D. 6
(A⁴)[0][0] = (2⁴ + 2·(−1)⁴)/3 = 6 closed walks. Walks ending at a fixed other vertex number 5, the trace over all starts is 18, and 16 counts every walk from 0.
More Coding Interview Problems questions
- Problem: How many ways are there to choose 4 distinct numbers from 1..12 with sum 26? Run the code. python from itertools import…
- Problem: What is the sum of the digits of the number 2^64? Run the code. python print(sum(int(c) for c in str(2 64))) What is printed?
- Problem: How many integers x in [1, 2000] satisfy x^2 ≡ x (mod 10)? Run the code. python print(sum(1 for x in range(1, 2001) if (x x - x)…
- Problem: How many inversions does the array [2, 3, 8, 6, 1] have? Run the code. python a = [2, 3, 8, 6, 1] print(sum(1 for i in…
- Problem: What is the number of distinct subarray sums of [1, 2, 3] (non-empty subarrays)? Run the code. python a = [1, 2, 3]…
- Problem: What is the sum of all two-digit numbers that are divisible by the sum of their digits? Run the code. python print(sum(x for x in…