PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates competence in binary tree data structures, spatial layout and string formatting by requiring conversion of a hierarchical tree into a constrained ASCII representation with placeholder handling for missing children; domain: Coding & Algorithms.

  • medium
  • Crusoe
  • Coding & Algorithms
  • Software Engineer

Print a binary tree as aligned text

Company: Crusoe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement a function that prints (or returns) an ASCII layout of a binary tree. Requirements: - Each node has a string value (assume it contains no spaces). - If a node is missing a left or right child, print a placeholder `*` in that child position. - The bottom row represents the leaf level of a *full* binary tree of height `h` (where `h` is the height of the input tree). It therefore has `2^(h-1)` leaf slots. - Leaf slots must be separated by exactly one space. - For every level above the leaves, each parent node must be positioned exactly in the middle (horizontally) between its left-child slot and right-child slot. Return the output as a list of strings (one per level from top/root to bottom/leaves), or print it line by line. Example (one possible formatting): If the tree is: - root = `A` - A.left = `B`, A.right = `C` - B.right = `D` (B.left is null) Then the bottom level has 4 slots: `* D * *` (with single spaces), and parents are centered above their two children slots. Explain any assumptions you make about node value width (e.g., single character vs variable-length strings).

Quick Answer: This question evaluates competence in binary tree data structures, spatial layout and string formatting by requiring conversion of a hierarchical tree into a constrained ASCII representation with placeholder handling for missing children; domain: Coding & Algorithms.

Render a binary tree as aligned ASCII text. You are given the tree in **level-order** (breadth-first) as a list of single-character string values, where the sentinel `"#"` marks an absent node. For example `["A", "B", "C", "#", "D"]` is the tree with root `A`, children `B` (left) and `C` (right), and `B` having only a right child `D`. Return the drawing as a **list of strings**, one per level from the root down to the leaves, obeying these rules: - The picture is laid out as a **full** binary tree of height `h` (the height of the input tree). The bottom (leaf) row therefore has `2^(h-1)` slots. - Each slot holds one character. A node that does not exist at a given position prints as `*`. - In the bottom row the slots are separated by exactly one space (so the row is `2*2^(h-1) - 1` characters wide). - For every level above the leaves, each parent is placed exactly in the middle (horizontally) between its left-child slot and its right-child slot. Padding to the left of a node's column is spaces. **Assumption:** node values are single characters with no spaces, so every slot is one column wide. (For variable-length values you would widen each slot to the longest value and center within it; that is out of scope here.) **Example** — input `["A", "B", "C", "#", "D"]` (height 3) returns: ``` A B C * D * * ```

Constraints

  • 1 <= number of input entries; level_order[0] != "#" for a non-empty tree.
  • Each present value is a single character with no spaces; "#" is reserved as the absent-node sentinel.
  • Input list is in level-order (BFS) and only includes positions down to the deepest present node.
  • Tree height h satisfies 2^(h-1) leaf slots fitting in memory (h is small in practice).

Examples

Input: ["A", "B", "C", "#", "D"]

Expected Output: [" A ", " B C ", "* D * *"]

Explanation: The prompt example. Height 3 -> 4 leaf slots. B has only a right child D, so its left leaf slot is '*'. C is missing both children -> '* *'. Parents B and C are centered above their child pairs, and A is centered above the whole row.

Input: ["A"]

Expected Output: ["A"]

Explanation: Single-node tree: height 1, one leaf slot, one line containing just the root.

Input: ["A", "B", "C"]

Expected Output: [" A ", "B C"]

Explanation: Full tree of height 2: 2 leaf slots 'B C' separated by one space, A centered above them.

Input: ["A", "B", "C", "D", "E", "F", "G"]

Expected Output: [" A ", " B C ", "D E F G"]

Explanation: A perfect tree of height 3. The leaf row 'D E F G' has each value one space apart; B sits above D/E and C above F/G; A is centered above all.

Input: ["A", "B", "#", "D"]

Expected Output: [" A ", " B * ", "D * * *"]

Explanation: A->B (A's right child is absent -> '*' in the upper-right). B->D (B's right child absent). Height 3, so the missing positions still occupy slots and print as '*'.

Input: ["A", "#", "C", "#", "F"]

Expected Output: [" A ", " * C ", "* * * F"]

Explanation: A has no left child ('*' on the left of the middle row) and a right child C; C has only a right child F (bottom-right slot). All absent positions render as '*'.

Input: []

Expected Output: []

Explanation: Empty input -> empty drawing.

Hints

  1. First rebuild the tree from the level-order array (skip a child when its value is "#"), then compute the height h.
  2. The bottom row is a full level with 2^(h-1) leaf slots; slot i sits at column 2*i, so the line is 2*2^(h-1) - 1 characters wide.
  3. Recurse positionally over a FULL tree: a node covering leaf slots [lo, hi] is drawn at the midpoint column (2*lo + 2*hi)/2; split into [lo, mid] and [mid+1, hi] for its children. Print '*' wherever the real node is missing.
Last updated: Jun 26, 2026

Related Coding Questions

  • Implement Interval Overrides and Top-K Strings - Crusoe (easy)
  • Calculate charge with a single price override - Crusoe (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.