Interview Format:
🎯 Problem Statement:
You are given an undirected connected graph that forms exactly one cycle (i.e., a ring). The graph is provided as an adjacency list. Your task is to print all the possible paths that form the complete ring, starting from each node.
Example Input:
graph = {
"A": ["B", "D"],
"B": ["C", "A"],
"C": ["D", "B"],
"D": ["A", "C"]
}
Expected Output:
ABCD
BCDA
CDAB
DABC
Key Constraints ( After verifying with the recuiter):
Discussion & Exploration:
The interviewer first asked the approach to solve it i gave brute force and using DFS.
Brute force solution explored all paths and maintained a full visited path to prevent cycles.
Optimization was then discussed:
Complexity Analysis:
Brute Force:
Optimized:
Interview Highlights:
The interviewer asked many deep questions:
They appreciated reasoning about:
Final Code (Optimized for Ring Graph):
def find_ring_paths(graph):
def dfs(node, parent, path):
path.append(node)
for neighbor in graph[node]:
if neighbor != parent:
dfs(neighbor, node, path)
break # only one forward direction to follow in a ring
for start in graph:
path = []
dfs(start, None, path)
print("".join(path))Key Take Aways: