← 返回 apple 的题目列表Kubernetes Services: Filtering, Dependency Chain Traversal, and Status Query
类型:online_judge
Coding: Kubernetes Services Filtering + Dependency Chain Traversal + Status Query
You are given a collection of Kubernetes service descriptors. Each service contains at least:
name: unique service name (string)
attributes: key/value metadata (string -> string)
status: one of Healthy / Degraded / Unhealthy / Unknown
dependencies: list of service names this service depends on (directed edge service -> dependency)
Implement three parts:
Part 1: Filtering
Implement filtering over the service list and return names (preserve input order) that satisfy all provided conditions (AND):
status_in: allowed set of statuses
attribute_equals: require a given attribute key to exist and equal a given value
name_prefix: match a name prefix
Part 2: Dependency chain + aggregated status
Given start_service, return:
reachable: all nodes reachable from start_service following dependencies (including start), in BFS visitation order
aggregated_status: aggregated status over the reachable set:
If any node is Unhealthy -> Unhealthy
Else if any is Degraded -> Degraded
Else if all are Healthy -> Healthy
Otherwise -> Unknown
Handle missing dependency references (ignore) and cycles (do not loop forever).
Part 3: Open-ended design
If you wrap this into an MCP server for an LLM Agent:
What tools/APIs would you expose?
How would you reduce noise and prevent misuse (validation, paging, top-k, sampling, thresholds, dedup, idempotency, rate limiting, caching, authz, etc.)?
Constraints
N services: 1..200000
E dependencies: 0..500000
Part2 may be called many times
I/O (for an online coding environment)
Assume JSON Lines:
Line 1: JSON array of services
Line 2: JSON object for filter conditions
Line 3: JSON string for start_service
Output:
Line 1: JSON array of filtered service names
Line 2: JSON object: { "reachable": [...], "aggregated_status": "..." }
Example
Input
[{"name":"api","attributes":{"team":"a"},"status":"Healthy","dependencies":["db","cache"]},{"name":"db","attributes":{"team":"a"},"status":"Degraded","dependencies":[]},{"name":"cache","attributes":{"team":"b"},"status":"Healthy","dependencies":[]}]
{"status_in":["Healthy"],"attribute_equals":{"key":"team","value":"a"},"name_prefix":"a"}
"api"
Output
["api"]
{"reachable":["api","db","cache"],"aggregated_status":"Degraded"}