Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Print Meta View Hierarchy

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Print Meta View Hierarchy. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

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=None, name=None, children=None):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output