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
Build an NPM Dependency Tree Program
00:00
5 left

Build an NPM Dependency Tree Program

MediumPython

Problem

Snyk analyzes NPM dependency graphs to help identify vulnerable transitive packages. Given a root package and its direct dependencies, produce a readable dependency tree using depth-first traversal.

Return a list of output lines. Each dependency must appear beneath its parent with two spaces of indentation per depth level. Preserve the dependency order supplied in the input. A package missing from the dependency map is a leaf package.

If a package appears again while it is already on the current traversal path, append (cycle) to that line and do not traverse its dependencies again. The same package may legitimately appear in multiple separate branches, so only packages on the current path count as cycles.

Formal Specification

Implement build_dependency_tree(root, dependencies), where root is a string and dependencies is a dictionary mapping package names to lists of direct dependency names. Return a list of strings, with the root as the first line. The caller can print the tree with print("\ ".join(result)).

Constraints

  • 1 <= number of reachable package occurrences <= 10^4
  • Package names are non-empty strings
  • Each dependency list preserves the required display order
  • The dependency graph may contain cycles
  • Package names may occur in multiple independent branches

Function Signature

def build_dependency_tree(root, dependencies):
Interviewer

Your question is Build an NPM Dependency Tree Program. 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.