1. [Graph] — Detect if there is a cycle in a directed graph
Background: Canonical often deals with complex distributed systems, like those involved in cloud and container orchestration. Detecting cycles in directed graphs is crucial for ensuring system reliability and avoiding deadlocks in processes. Problem statement: Given a directed graph represented as an adjacency list, implement a function to determine if the graph contains a cycle. A graph is defined as cyclic if there exists a path which starts and ends at the same vertex. Your graph will be represented as a List[List[int]] where each index represents a vertex and the list at that index contains the vertices it points to. Function/class signature:
def has_cycle(graph: List[List[int]]) -> bool:
Example 1:
Input: graph = [[1], [2], [0]]
Output: True
Explanation: There is a cycle: 0 -> 1 -> 2 -> 0.
Example 2:
Input: graph = [[1], [2], []]
Output: False
Explanation: There are no cycles, as all vertices point to the next in a linear fashion.
Constraints:
The number of vertices V is between 1 and 10^4.
Each vertex has at most V-1 edges to other vertices.
codingHarddistributed systems#2
2. Coding — Debugging in a Distributed System
1. Background: In Canonical's cloud infrastructure, effective debugging is critical for maintaining system reliability and performance. Understanding how to troubleshoot complex issues is essential for ensuring seamless user experiences. 2. Problem statement: You are tasked with debugging a bug in a distributed system where services communicate over HTTP. You receive reports that one of your microservices occasionally fails to respond. Walk through your debugging process step by step, detailing the tools and methods you would utilize to isolate and resolve the issue. 3. Function/class signature: - def debug_issue(service_name: str) -> str: - Takes in the name of the service and returns the debugging outcome as a string. - def analyze_logs(logs: List[str]) -> List[str]: - Takes in raw logs and returns identified issues as a list of strings. - def check_service_health(service_name: str) -> bool: - Checks the health of the service and returns a boolean. 4. Example 1: - Input: debug_issue('payment_service') - Output: 'Issues resolved: High latency, timeout errors on request.' - Explanation: The system identifies latency issues and timeout errors as the root causes for the service failure. 5. Example 2: - Input: debug_issue('user_profile_service') - Output: 'Issues resolved: No existing errors in logs, service is healthy.' 6. Constraints: - No more than 10 services can be debugged at once. - Each log entry is a string of maximum 256 characters. - Service names can have a maximum length of 100 characters.
Start practicing Canonical questions
Sign up for free to access walkthroughs, AI-generated questions, and more.