Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: Does an undirected connected graph have an Euler path / Euler circuit? (count odd-degree vertices)
Problem: Does an undirected connected graph have an Euler path / Euler circuit? (count odd-degree vertices)
from collections import Counter
def euler(edges):
deg = Counter()
for u, v in edges: deg[u] += 1; deg[v] += 1
odd = sum(1 for d in deg.values() if d % 2)
return "circuit" if odd == 0 else "path" if odd == 2 else "neither"
print(euler([(0,1),(1,2),(2,0),(0,3),(3,4),(4,0)]), euler([(0,1),(1,2),(2,3),(3,0),(0,2)]), euler([(0,1),(0,2),(0,3)]))What is printed?- A. circuit circuit neither
- B. circuit path neither
- C. path path neither
- D. circuit path path
Correct answer: B. circuit path neither
Graph 1 (two triangles sharing node 0): all degrees even → Euler circuit. Graph 2 (a square with one diagonal): nodes 0 and 2 have degree 3 → Euler path from 0 to 2. Graph 3 (a star with 3 leaves): four odd vertices → neither.
More Coding Interview Problems questions
- Problem: Count Hamiltonian paths (ordered sequences visiting every vertex once) in a small undirected graph with bitmask DP. python n = 5…
- Problem: Reconstruct an actual longest strictly increasing subsequence with the O(n²) DP and parent pointers (the first maximal end index…
- Problem: Minimum number of single-character insertions plus deletions to turn a into b (no replacements). python def insdel(a, b): m, n =…
- Problem: Fibonacci modulo 10⁹ + 7 by 2 × 2 matrix exponentiation in O(log n). python MOD = 109 + 7 def mul(A, B): return [[(A[0][0]B[0][0]…
- Problem: Count balanced parenthesis strings with n pairs whose nesting depth never exceeds d (DP over position and current depth). python…
- Problem: Count integers in [1, N] whose digits sum to S, with a digit DP over (position, sum so far, tight). python from functools import…