PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Crusoe

Print a binary tree as aligned text

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Implement Interval Overrides and Top-K Strings - Crusoe (easy)
  • Calculate charge with a single price override - Crusoe (medium)
Crusoe logo
Crusoe
Jan 1, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
11
0

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).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Crusoe•More Software Engineer•Crusoe Software Engineer•Crusoe Coding & Algorithms•Software Engineer Coding & Algorithms
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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.