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?
  1. A. circuit circuit neither
  2. B. circuit path neither
  3. C. path path neither
  4. 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

Exams that ask this topic