Build and Render an Organization Role Tree
Problem statement
Build and render an organizational hierarchy from unordered role relations.
Each relation has the form role,parent. Exactly one relation has an empty parent and defines the root. Every other parent names another role in the input.
Return the hierarchy in preorder. Render each role as:
<two hyphens repeated depth times><role>The root has depth 0. Sort every role's direct children in ascending lexicographic order before traversal.
What the interview report shared
The reported internship OA progressively managed and printed a tree of company roles. The exact operations, traversal order, input format, and print layout were not preserved.
Function
renderOrganization(relations: String[]) → String[]Examples
Example 1
relations = ["engineer,manager","ceo,","manager,ceo","designer,manager","finance,ceo"]return = ["ceo","--finance","--manager","----designer","----engineer"]The root's children are sorted as finance then manager. The manager's children are sorted as designer then engineer.
Example 2
relations = ["intern,engineer","engineer,lead","lead,"]return = ["lead","--engineer","----intern"]The unordered relations form one chain with depths 0, 1, and 2.
Constraints
1 <= relations.length <= 10^5.- Every role is unique and contains lowercase English letters, digits, or underscores.
- Exactly one role has an empty parent.
- Every nonroot parent exists, and the relations form one valid tree.
- Relations may arrive in any order.