KatInterview

Service Inventory Filter and Dependency Risk Report

Trees & GraphsAuto-graded · 14 test cases
MediumAuto-gradedTrees & Graphsgraph-traversaldfshash-map

Last updated

Filter a service inventory by namespace, attributes and status, then compute transitive dependencies and risk flags from a root service, handling cycles and dangling references.

You are given a list of service records describing a deployment. Each record is a dictionary with these keys:

  • id: a unique string such as "namespace/name"
  • name: a string
  • namespace: a string
  • attributes: a dictionary mapping string keys to string values
  • status: one of "Healthy", "Degraded", "Down", "Unknown"
  • dependencies: a list of service IDs that this service depends on

Write a single function that takes the list of services, a criteria dictionary, and a root_id string, and returns a dictionary with three keys: filtered, dependency_chain, and status_chain.

Part 1: filtered

criteria may contain any of the following optional keys:

  • namespace: if present (and not None), a service must have exactly this namespace.
  • attributes: a dictionary of required key/value pairs. A service matches only if every pair appears in its own attributes. A missing key counts as a mismatch. If this key is omitted or empty, there is no attribute constraint.
  • statuses: a list of allowed statuses. If this key is omitted or None, any status is allowed. If it is an empty list, no service matches.

A service must satisfy all of the given criteria. filtered contains the full original service records that match, sorted ascending by id. An empty criteria matches every service.

Part 2: dependency_chain

Find every service that is transitively reachable from root_id by following dependencies. The root itself is not included unless it can be reached again through a cycle. Rules:

  • Dependency IDs that do not exist in the inventory are ignored; they never appear in the output and never cause an error.
  • Cycles (including self-dependencies) must not cause infinite loops.
  • Each reachable service appears exactly once, even if reachable by multiple paths or listed multiple times.
  • If root_id is not in the inventory, the result is an empty list.

dependency_chain is a list of dictionaries {"id": ..., "status": ...}, sorted ascending by id.

Part 3: status_chain

Report the status of the root and every service reachable from it. Each entry is a dictionary:

{"id": ..., "status": ..., "may_affect_root": ...}
  • The set of services is every service in dependency_chain, plus the root itself (if it exists in the inventory). Each service appears once.
  • may_affect_root is True if the service is not the root and its status is "Degraded" or "Down"; otherwise False. The root is never flagged.
  • Entries are sorted ascending by id.
  • If root_id is not in the inventory, the result is an empty list.

Constraints

  • Sorting by id is ordinary string comparison.
  • Service IDs are unique.
  • Records may omit attributes or dependencies; treat a missing one as empty.
  • Do not mutate the input.
  • The dependency graph can be deep, so avoid relying on deep recursion.

Examples

Example 1

Input: analyze_services([{'id': 'app/web', 'name': 'web', 'namespace': 'app', 'attributes': {'tier': 'frontend'}, 'status': 'Healthy', 'dependencies': ['app/cache', 'app/db']}, {'id': 'app/cache', 'name': 'cache', 'namespace': 'app', 'attributes': {'tier': 'data'}, 'status': 'Degraded', 'dependencies': ['app/db']}, {'id': 'app/db', 'name': 'db', 'namespace': 'app', 'attributes': {'tier': 'data'}, 'status': 'Healthy', 'dependencies': []}], {'namespace': 'app', 'attributes': {'tier': 'data'}, 'statuses': ['Healthy', 'Degraded']}, 'app/web')

Output: {'filtered': [{'id': 'app/cache', 'name': 'cache', 'namespace': 'app', 'attributes': {'tier': 'data'}, 'status': 'Degraded', 'dependencies': ['app/db']}, {'id': 'app/db', 'name': 'db', 'namespace': 'app', 'attributes': {'tier': 'data'}, 'status': 'Healthy', 'dependencies': []}], 'dependency_chain': [{'id': 'app/cache', 'status': 'Degraded'}, {'id': 'app/db', 'status': 'Healthy'}], 'status_chain': [{'id': 'app/cache', 'status': 'Degraded', 'may_affect_root': True}, {'id': 'app/db', 'status': 'Healthy', 'may_affect_root': False}, {'id': 'app/web', 'status': 'Healthy', 'may_affect_root': False}]}

Example 2

Input: analyze_services([], {}, 'missing')

Output: {'filtered': [], 'dependency_chain': [], 'status_chain': []}

Hints

Hint 1

Build a dictionary from id to service once, so lookups and detection of missing dependency IDs are O(1).

Hint 2

Treat the three outputs as independent computations: a filter with all conditions combined, then one graph reachability search whose result feeds both chain outputs.

Hint 3

Use an iterative DFS or BFS with a visited set; seed it with the root's direct dependencies (not the root itself) and skip IDs that are already visited or not in the inventory.

Hint 4

Distinguish statuses being None (no restriction) from an empty list (nothing allowed), and make sure the root is added to the status report separately without ever being flagged.