Dependency-Safe Deletion Order

Quick Overview

Return the lexicographically smallest child-before-parent deletion order under additional precedence constraints, or detect a cycle.

Dependency-Safe Deletion Order

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Dependency-Safe Deletion Order Implement `dependency_safe_deletion_order(parents: list[int], constraints: list[list[int]]) -> list[int]`. There are `n = len(parents)` entities labeled `0` through `n - 1`. `parents[i]` is the parent of entity `i`, or `-1` if `i` has no parent. A child must be deleted before its parent. Each explicit constraint `[a, b]` additionally requires `a` to be deleted before `b`. Return a deletion order satisfying every requirement. ### Input Domain - `1 <= n <= 200,000`. - Parent links form a forest and contain no self-link. - `0 <= len(constraints) <= 300,000`. - Each constraint names two distinct valid entities; duplicate requirements may occur. ### Output Rules - If several entities are currently eligible, always choose the smallest label. - Return the lexicographically smallest valid deletion order. - Return an empty list if the combined requirements contain a cycle. - Every entity must appear exactly once in a nonempty valid result. ### Constraints - Treat `child before parent` and each explicit pair as directed precedence edges. - Target time is `O((n + m) log n)` for `m` distinct precedence edges. ### Examples #### Example 1 Input: `parents = [-1,0,0,1], constraints = [[2,1]]` Output: `[2,3,1,0]` #### Example 2 Input: `parents = [-1,0], constraints = [[0,1]]` Output: `[]` ```hint Eligibility comes from incoming requirements Build one precedence graph, track each node's unmet predecessor count, and use a smallest-first collection for eligible nodes. ```

Quick Answer: Return the lexicographically smallest child-before-parent deletion order under additional precedence constraints, or detect a cycle.

|Home/Coding & Algorithms/Google
Google logo
Google
Aug 18, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
18
0

Dependency-Safe Deletion Order

Implement dependency_safe_deletion_order(parents: list[int], constraints: list[list[int]]) -> list[int].

There are n = len(parents) entities labeled 0 through n - 1. parents[i] is the parent of entity i, or -1 if i has no parent. A child must be deleted before its parent. Each explicit constraint [a, b] additionally requires a to be deleted before b. Return a deletion order satisfying every requirement.

Input Domain

  • 1 <= n <= 200,000 .
  • Parent links form a forest and contain no self-link.
  • 0 <= len(constraints) <= 300,000 .
  • Each constraint names two distinct valid entities; duplicate requirements may occur.

Output Rules

  • If several entities are currently eligible, always choose the smallest label.
  • Return the lexicographically smallest valid deletion order.
  • Return an empty list if the combined requirements contain a cycle.
  • Every entity must appear exactly once in a nonempty valid result.

Constraints

  • Treat child before parent and each explicit pair as directed precedence edges.
  • Target time is O((n + m) log n) for m distinct precedence edges.

Examples

Example 1

Input: parents = [-1,0,0,1], constraints = [[2,1]]

Output: [2,3,1,0]

Example 2

Input: parents = [-1,0], constraints = [[0,1]]

Output: []

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...