Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many paths of length 3 exist in a complete graph K4 that start and end at different vertices? Run the code (count ordered walks v0, v1, v2, v3 with v0 != v3 and consecutive vertices distinct).
Problem: How many paths of length 3 exist in a complete graph K4 that start and end at different vertices? Run the code (count ordered walks v0, v1, v2, v3 with v0 != v3 and consecutive vertices distinct).
n = 4 print(sum(1 for a in range(n) for b in range(n) for c in range(n) for d in range(n) if a != b and b != c and c != d and a != d))What is printed?
- A. 108
- B. 24
- C. 84
- D. 256
Correct answer: C. 84
Walks with distinct neighbours: 4 · 3 · 3 · 3 = 108, minus the 24 closed walks with v₀ = v₃, gives 84. 108 forgets the end condition, 24 counts paths with all vertices distinct, and 4⁴ = 256 ignores every constraint.
More Coding Interview Problems questions
- Problem: What is the number of integers in [1, 1000] whose digits are all odd? Run the code. python print(sum(1 for x in range(1, 1001) if…
- Problem: What is the length of the longest subarray of [5, 1, 4, 2, 3, 6] whose elements can be sorted to form consecutive integers? Run…
- Problem: How many integers from 1 to 1000 are not divisible by any of 2, 3, 5 and 7? Run the code. python print(sum(1 for x in range(1…
- Problem: What is the maximum sum of a subsequence (not necessarily contiguous) of [4, -2, 5, -1, 3] with no two chosen elements adjacent?…
- Problem: How many ways are there to write 12 as a sum of distinct positive integers (partitions into distinct parts)? Run the DP. python…
- Problem: What is the number of trailing zeros of 25 40 125? Run the code. python n = 25 40 125 c = 0 while n % 10 == 0: n //= 10 c += 1…