FastPrepBuild and Render an Organization Role Tree
Problems/Build and Render an Organization Role Tree

Build and Render an Organization Role Tree

Stripe logoStripe● MediumINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source preserves a progressive organization-role tree and printing objective but omits operations, traversal order, mutation rules, input, output, and bounds. The judged core task matches the report at about 48%.

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 problems

  • Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
  • Business Account KYC Verification — Parts 1–5PHONE SCREEN · Seen Nov 2026
  • Render a Task Tree from CSV RowsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
All Stripe interview questionsSee Stripe hiring insights
Saved
class Solution {
    public String[] renderOrganization(String[] relations) {
        // write your code here
    }

}
Checking account…