Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Print Meta View Hierarchy

EasyPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Print Meta View Hierarchy. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

In a Meta mobile app such as Facebook or Instagram, UI debugging often requires inspecting the full view tree from a root view. Given the root of a view hierarchy, return a formatted list of strings representing the entire hierarchy in top-down order, with indentation showing parent-child relationships.

Each view has a name string and a list of children. Write a function that traverses the hierarchy and returns one string per view. The root should have no indentation, its children should be prefixed with two spaces, grandchildren with four spaces, and so on.

Formal Specification

  • Input: root, a view node object or None
  • View node fields:
    • name: string
    • children: list of child view nodes
  • Output: a list of strings representing the hierarchy

Constraints

  • The number of views is in the range [0, 10^4]
  • 1 <= len(view.name) <= 100
  • Each node has 0 to 100 children
  • The input hierarchy is a tree with no cycles

Function Signature

def print_view_hierarchy(root):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output