Build and Render an Organization Role Tree
Updated
Problem statement
Build a company’s role tree from relations given in any order, then return its text representation.
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.
Visit each role before its children. Visit each child’s entire subtree before moving to the next child, using the sorted child order described below. Write 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.
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.
More Stripe Online Assessment questions
- Split Tender Part 4 - RefundsOA · PHONE SCREEN · Seen Nov 2026
- Split Tender Part 3 - SurchargesOA · Seen Nov 2026
- Split Tender Part 2 - Expiration DatesOA · Seen Nov 2026
- Split Tender Part 1 - Basic Split TenderingOA · Seen Nov 2026
- Asynchronous Event ProcessingOA · Seen Nov 2026
- Subscription and Usage-Based Billing CalculatorOA · Seen Nov 2026
- Payment Partner RoutingOA · Seen Nov 2026
- Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
- Maximum Game Score with Fixed Forward JumpsOA · Seen Nov 2026
- Reliable LLM Call with Independent Retry BudgetsOA · Seen Nov 2026
Similar questions at other companies
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Count Distinct Rooted-Tree GraftsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Merge Row Trees by Key PathHudson River Trading · OAHudson River Trading interview questions
- Get Most Hydrated TeamAirtable · OAAirtable interview questions
- Resolve Inherited Server ConfigurationIntuit · OAIntuit interview questions
- Palindromic Paths to the RootRippling · OARippling interview questions
- Find PalindromesGoogle · OAGoogle interview questions
- Validate BST Node DescriptionsAirbnb · OAAirbnb interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions