Feature Deployment with Integrity Checks and Dependency Ordering
Trees & GraphsAuto-graded · 21 test casesLast updated
Verify configuration integrity using polynomial hashing, detect dependency cycles with DFS, and produce a lexicographically-ordered topological deployment sequence.
You're implementing a deployment system that validates and orders feature modules based on their integrity and dependencies.
Each configuration entry in configs is formatted as:
"<name>:<checksum>:<dependencies>"
Where:
<name>is the feature identifier<checksum>is a decimal string representing the expected integrity value<dependencies>is a comma-separated list of required features (empty if none)
Example: "auth:431127:core" means feature auth requires core and has checksum 431127.
Integrity Verification
For each feature name, calculate its integrity value using:
hash = sum(ord(char) * (37 ** position) for position, char in enumerate(name)) % 1000003
Compute this incrementally to avoid overflow. A feature is corrupted if the calculated hash doesn't match the provided checksum value exactly (as decimal strings).
Deployment Eligibility
A feature is deployable only when:
- Its configuration is not corrupted (checksum matches)
- All features it depends on exist and are deployable
- No circular dependencies exist in its dependency chain
Output Requirements
From the requested list, identify all deployable features. Return a deployment sequence containing these features plus all their transitive dependencies, ordered such that:
- Every dependency appears before the features that require it
- Each feature appears exactly once
- When multiple features have all dependencies satisfied, choose the lexicographically smallest one first
Return an empty list if no requested features are deployable.
Constraints
1 <= len(configs) <= 10^41 <= len(requested) <= 10^3- Each feature name is a non-empty string
- Checksum values are non-empty decimal digit strings
- Feature names in
configsare unique - Target complexity: O(V + E log V) where V = number of features, E = dependency relationships
Examples
Example 1
Input: deploy_features(['auth:431127:core', 'core:276210:'], ['auth'])
Output: ['core', 'auth']
Example 2
Input: deploy_features(['a:97:b', 'b:98:a', 'c:99:'], ['a', 'c'])
Output: ['c']
Example 3
Input: deploy_features(['a:97:', 'x:120:a', 'y:121:a', 'q:113:a,x'], ['q', 'y'])
Output: ['a', 'x', 'q', 'y']
Hints
Hint 1
Calculate the polynomial hash incrementally with modulo at each step to prevent integer overflow
Hint 2
Use depth-first search with an active-set to detect cycles: if you visit a node already on the current recursion path, you've found a cycle
Hint 3
Apply Kahn's topological sort algorithm with a min-heap to ensure lexicographically smallest ordering among available features
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem combines three graph algorithms: integrity verification through hashing, cycle detection via depth-first search, and lexicographic topological ordering.
Phase 1: Parse and Verify Integrity
Parse each configuration string by splitting on colons. Calculate the polynomial rolling hash for the feature name using base 37 and modulo 1000003. Compute this incrementally to avoid large intermediate values. If the calculated hash doesn't match the provided checksum (compared as strings), mark the feature as corrupted by storing None in the dependency graph. Skip malformed entries entirely.
Phase 2: Validate with Cycle Detection
Use memoized depth-first search to determine which features are deployable. Maintain an active set tracking the current recursion path. If we encounter a feature already in active, we've detected a cycle and return False. A feature is deployable only if it's not corrupted, all its dependencies are deployable, and no cycles exist. Memoization ensures each feature is evaluated once.
Phase 3: Topological Sort with Lexicographic Ordering
First, collect the transitive closure of all deployable requested features using DFS. Then apply Kahn's algorithm using a min-heap instead of a queue. The heap ensures that among all features with satisfied dependencies, we always select the lexicographically smallest one next. Build an indegree map and reverse adjacency list, initialize the heap with zero-indegree nodes, and process nodes one by one, decrementing dependent indegrees as we go.
Why This Works
The active set implements the "gray" state in three-color DFS, detecting back edges that indicate cycles. Memoization prevents redundant work and ensures O(V + E) traversal time. The transitive closure guarantees we include all necessary dependencies. Kahn's algorithm with a heap produces a valid topological order while respecting the lexicographic constraint, and the algorithm completes successfully only on DAGs.
Complexity
Time: O(L + V + E log V)
- L is total configuration string length (parsing and hashing)
- V + E for DFS validation and closure collection
- V log V for heap operations during topological sort (each of V nodes is pushed/popped once)
Space: O(V + E) for the dependency graph, memoization cache, reverse adjacency list, and recursion stack.
import heapq
import sys
def deploy_features(configs, requested):
MOD = 1000003
sys.setrecursionlimit(1000000)
def compute_hash(name):
value = 0
power = 1
for ch in name:
value = (value + ord(ch) * power) % MOD
power = power * 37 % MOD
return value
graph = {}
for raw in configs:
try:
name, checksum_text, dependency_text = raw.split(':', 2)
if int(checksum_text) != compute_hash(name) or str(compute_hash(name)) != checksum_text:
graph[name] = None
continue
deps = [] if dependency_text == '' else dependency_text.split(',')
graph[name] = deps
except (ValueError, TypeError):
continue
memo = {}
active = set()
def is_deployable(name):
if name in memo:
return memo[name]
if name in active:
return False
deps = graph.get(name)
if deps is None:
memo[name] = False
return False
active.add(name)
result = all(is_deployable(dep) for dep in deps)
active.remove(name)
memo[name] = result
return result
roots = []
for name in requested:
if is_deployable(name):
roots.append(name)
selected = set()
def add_transitive(name):
if name in selected:
return
selected.add(name)
for dep in graph[name]:
add_transitive(dep)
for name in roots:
add_transitive(name)
if not selected:
return []
indegree = {name: 0 for name in selected}
reverse = {name: [] for name in selected}
for name in selected:
for dep in graph[name]:
indegree[name] += 1
reverse[dep].append(name)
heap = [name for name, degree in indegree.items() if degree == 0]
heapq.heapify(heap)
result = []
while heap:
name = heapq.heappop(heap)
result.append(name)
for dependent in reverse[name]:
indegree[dependent] -= 1
if indegree[dependent] == 0:
heapq.heappush(heap, dependent)
return result