Deploy pipeline chain

Write codeEasy17 min

Your task

A deploy pipeline is stored the way a linked list is stored in a flat table. Step i is named names[i], and next_step[i] holds the index of the step that runs immediately after step i, or -1 when step i is the last one. The two lists have the same length, and every entry of next_step is either -1 or a valid index, possibly the step's own index. A healthy pipeline is one single chain: exactly one step has nothing pointing at it, and following the chain from that step reaches every step exactly once. Return the step names in execution order. Return an empty list when the table is not one healthy chain, which covers two steps claiming the same successor, a count of starting steps other than one, and steps sitting in a loop the start never reaches.

Examples

pipeline_step_order(names: list[str], next_step: list[int]) -> list[str]
Example 1
Inputpipeline_step_order(["build", "test", "ship"], [1, 2, -1])
Output["build", "test", "ship"]
Example 2
Inputpipeline_step_order(["build", "test", "ship"], [-1, 0, 1])
Output["ship", "test", "build"]
Example 3
Inputpipeline_step_order(["a", "b"], [1, 0])
Output[]