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
Hierarchical Box Insertion
00:00
5 left

Hierarchical Box Insertion

MediumPython

Problem

There are boxes of different sizes (small, medium, large); a small box fits in a medium and a medium in a large. Implement insertBox(Box b1, Box b2) to insert b1 into b2, and printBoxes() to print all boxes in hierarchical order.

Asked in the LLD round stage. Pair programming; focus on modularity, extensibility and abstractions rather than working code.

Use insertBox(boxes, b1, b2), where boxes maps box IDs to {size, parent} records and parent is either another ID or null. Insert b1 as a child of b2, then return IDs in preorder, starting with each root.

Constraints

  • 2 <= number of boxes <= 10^4
  • Each box size is one of small, medium, or large
  • A small box may be inserted into a medium or large box
  • A medium box may be inserted into a large box
  • b1 and b2 are distinct existing box IDs
  • The insertion is valid and does not create a cycle
  • Each box has at most one parent

Function Signature

def insertBox(boxes, b1, b2):
Interviewer

Your question is Hierarchical Box Insertion. 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.