Quick Overview

This question evaluates proficiency with representing and traversing hierarchical directory trees and producing depth-based formatted output, testing concepts such as tree traversal, recursion or iterative depth tracking, and string formatting for indentation.

Print directory tree with indentation

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a root directory of a filesystem represented as a tree. Each node is either a **directory** (can have children) or a **file** (no children). Print the directory structure starting from the root, using indentation to show depth. ### Output format - Print one node per line. - The root is printed with **no indentation**. - For each depth level below the root, prefix the name with an indentation marker (for example: `⟶ `). Repeating the marker indicates deeper nesting. Example formatting (depth shown by repeated markers): ``` dir ⟶ subdir1 ⟶ ⟶ file1.ext ⟶ ⟶ subsubdir1 ⟶ subdir2 ⟶ ⟶ subsubdir2 ⟶ ⟶ ⟶ file2.ext ``` ### Requirements / assumptions - You may assume the input is already a tree (no cycles). - The children of a directory should be printed in the order they are provided. ### Task Implement a function that, given the root node, prints (or returns as a list of strings) the directory tree in the required format.

Quick Answer: This question evaluates proficiency with representing and traversing hierarchical directory trees and producing depth-based formatted output, testing concepts such as tree traversal, recursion or iterative depth tracking, and string formatting for indentation.

Implement solution(root, indent_marker="-> ") that returns one line per node. root may be a dict with name and children or a [name, children] pair. Children must be emitted in the provided order.

Constraints

  • The input is already a tree
  • Files have no children

Examples

Input: ({'name': 'dir', 'children': [{'name': 'subdir1', 'children': [{'name': 'file1.ext', 'children': []}, {'name': 'subsubdir1', 'children': []}]}, {'name': 'subdir2', 'children': [{'name': 'subsubdir2', 'children': [{'name': 'file2.ext', 'children': []}]}]}]},)

Expected Output: ['dir', '-> subdir1', '-> -> file1.ext', '-> -> subsubdir1', '-> subdir2', '-> -> subsubdir2', '-> -> -> file2.ext']

Input: (['root', [['file', []]]], ' ')

Expected Output: ['root', ' file']

Hints

  1. Depth-first traversal naturally preserves child order.

Loading coding console...