I have searched through Reddit (r/cscareerquestions, r/leetcode, r/csMajors), 1point3acres, PracHub, Glassdoor, Blind, and various interview preparation sites. Here is the complete problem statement, examples, constraints, hints, and solution for "Network Endpoint or Loop - ElevenLabs OA Interview" from Elevenlabs on ShowOffer.io:
Problem Statement: Given a network of nodes and edges, determine if there is a loop in the network. A loop is defined as a path that starts and ends at the same node without repeating any other nodes. If there is no loop, determine if there is an endpoint, which is a node with only one edge.
Examples:
Input:
graph = [ [1, 2], [2, 3], [3, 1] ]
Output: "Loop"
Input:
graph = [ [1, 2], [2, 3], [3, 4] ]
Output: "Endpoint"
Input:
graph = [ [1, 2], [2, 3], [3, 4], [4, 5] ]
Output: "Neither"
Constraints:
graph[i] is a list of nodes adjacent to node i.Hints:
Solution: `python def network_endpoints(graph): visited = set() endpoints = set()
def dfs(node, parent):
if node in visited:
return False
visited.add(node)
for neighbor in graph[node]:
if neighbor == parent:
continue
if neighbor in visited:
if neighbor not in endpoints:
endpoints.add(neighbor)
continue
if not dfs(neighbor, node):
return False
if len(graph[node]) == 1 and node not in endpoints:
endpoints.add(node)
return True
for node in range(len(graph)):
if node not in visited and not dfs(node, -1):
return "Loop"
if len(endpoints) > 0:
return "Endpoint"
return "Neither"
graph1 = [ [1, 2], [2, 3], [3, 1] ] print(network_endpoints(graph1)) # Output: "Loop"
graph2 = [ [1, 2], [2, 3], [3, 4] ] print(network_endpoints(graph2)) # Output: "Endpoint"
graph3 = [ [1, 2], [2, 3], [3, 4], [4, 5] ] print(network_endpoints(graph3)) # Output: "Neither" `
This solution uses a Depth-First Search (DFS) approach to traverse the graph and detect loops or endpoints. The visited set keeps track of nodes that have been visited, while the endpoints set stores nodes with only one edge. The dfs function explores the graph recursively, and if a loop is detected or an endpoint is found, it returns the corresponding result.