Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
B-Tree Height Coding Problem
00:00
5 left

B-Tree Height Coding Problem

EasyPython

Problem

Yahoo Search organizes some in-memory hierarchical structures as balanced multiway trees. Given the root of a B-Tree, write a function that returns its height.

A B-Tree node is represented as a dictionary with a keys list and a children list. A leaf has an empty children list. For this problem, height is measured as the number of levels: an empty tree has height 0, and a tree containing only the root has height 1.

Because a valid B-Tree has all leaves at the same depth, every non-empty child subtree has the same height. You may compute the height by following any child, but your solution should traverse children in a way that remains correct for a valid B-Tree.

Formal Specification

Implement height(root), where root is either None or a dictionary of the form {"keys": [...], "children": [...]}. Return an integer representing the tree height. Keys may be any JSON-compatible values and do not affect the result.

Example 1: root = None returns 0 because the tree is empty.

Example 2: A root with two leaf children returns 2, because the tree has a root level and a leaf level.

Constraints

  • The tree contains at most 10^5 nodes.
  • Every internal node has at least two children.
  • Each internal node has exactly len(keys) + 1 children.
  • All leaves are at the same depth.
  • The input is a valid B-Tree.

Function Signature

def height(root):
Interviewer

Your question is B-Tree Height Coding Problem. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.