Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.