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
Detect Cyclic Package Imports
00:00
5 left

Detect Cyclic Package Imports

MediumPython

Problem

Implement an algorithm to detect cyclic dependencies in a list of package imports.

Represent imports as a dictionary mapping each package name to a list of packages it imports. Return True if the directed dependency graph contains a cycle, including a self-import, and False otherwise. Packages mentioned only as dependencies should be treated as valid leaf nodes.

Function contract: def has_cyclic_dependencies(imports): accepts dict[str, list[str]] and returns a boolean.

Constraints

  • 0 <= len(imports) <= 1000
  • Package names are non-empty strings
  • Each import list contains zero or more package names
  • A dependency may appear in an import list without being a key in imports
  • The total number of listed import relationships is at most 10000

Function Signature

def has_cyclic_dependencies(imports):
Interviewer

Your question is Detect Cyclic Package Imports. 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.