KatInterview

Feature Deployment with Integrity Checks and Dependency Ordering

Trees & GraphsAuto-graded · 21 test cases
HardAuto-gradedTrees & Graphsgraph-traversaltopological-sorthashing

Last 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:

  1. Its configuration is not corrupted (checksum matches)
  2. All features it depends on exist and are deployable
  3. 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^4
  • 1 <= len(requested) <= 10^3
  • Each feature name is a non-empty string
  • Checksum values are non-empty decimal digit strings
  • Feature names in configs are 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