Kubernetes service dependency queries
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Apple's coding round, last asked Feb 2026. It tests graphs, dependencies and design.
You get an in-memory inventory of Kubernetes-style services. Each service has an id (such as "api/orders"), a name, a namespace, a map of string attributes, a status ("Healthy", "Degraded", "Down" or "Unknown") and a list of dependencies, the IDs of services it calls. Write a small query library over it, one part at a time.
Part 1: filter
-
filter_services(services, criteria) -> list[str]:criteriamay hold anamespace,required_attributes(every key and value must match) andallowed_statuses. A filter that is missing does not restrict anything; an emptyallowed_statusesmatches nothing. Return the matching IDs sorted.
Part 2: transitive dependencies
-
dependency_chain(services, root_id) -> list[dict]: every service reachable fromroot_id, each once, as{"id", "status"}, in depth-first order following each service's list in order. Leave out the root, even if a cycle leads back to it. A dependency ID that is not in the inventory appears with status"Missing". An unknown root returns[].
Part 3: status along every path
-
status_paths(services, root_id) -> list[list[dict]]: every path from the root, each a list of{"id", "status"}starting with the root. A path ends at a service with no dependencies, at a missing ID (status"Missing"), or at a service already on that path (status"Cycle"). The same service may appear on several paths.
Part 4: trim results for an AI agent
The library is exposed as tools to an AI agent. The agent ran a broad filter and holds candidate IDs. Implement relevant(services, root_id, candidate_ids, max_results): keep candidates that exist and are reachable from the root (the root itself counts, at distance 0), drop duplicates, and return {"id", "status", "distance"} with the shortest distance. Sort by severity (Down, Degraded, Unknown, Healthy), then distance, then ID, and return at most max_results.
Example
web/frontend Healthy deps: api/orders, api/auth
api/orders Degraded deps: db/postgres, api/auth attrs: tier=backend, team=shop
api/auth Healthy deps: db/redis attrs: tier=backend
db/postgres Down deps: api/orders
filter_services(..., {namespace: "api", required_attributes: {tier: backend},
allowed_statuses: [Healthy, Degraded]})
-> ["api/auth", "api/orders"]
dependency_chain(..., "web/frontend")
-> api/orders Degraded, db/postgres Down, api/auth Healthy, db/redis Missing
status_paths(..., "web/frontend")
-> frontend > orders > postgres > orders (Cycle)
frontend > orders > auth > redis (Missing)
frontend > auth > redis (Missing)
relevant(..., "web/frontend",
["db/redis", "db/postgres", "api/auth", "api/orders", "api/orders", "web/frontend"], 3)
-> db/postgres Down 2, api/orders Degraded 1, web/frontend Healthy 0
Constraints
- Up to 10,000 services and 20,000 dependency edges; IDs are unique.
- The graph may have cycles and references to services that don't exist.
Clarifying questions
- Does a missing filter mean "any", and does an empty status list mean "none"?
- Should the root appear in its own chain when a cycle returns to it?
- How are missing dependencies reported, and does traversal stop there?
- Can the number of paths in Part 3 blow up, and is that acceptable?
What a strong answer covers
- An ID-to-service map built once, so lookups are O(1).
- A visited set for Part 2 (O(V + E)); an on-path set with backtracking for Part 3, and why its output can be exponential.
- BFS for shortest distances in Part 4, filtering before sorting.
- Deterministic ordering everywhere, and tests for cycles, self-loops, missing IDs and an unknown root.
Follow-ups
- As tools for an agent: which tools you expose, what each returns, and how you keep results short and relevant (reachability, severity ranking, limits, pagination).
- Statuses change constantly: what you cache and what you recompute.
- Answer "which services are affected if this one goes down" (reverse edges).
Where it's asked
- Apple · Coding · Last asked Feb 2026
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
