Skip to the code
cf.
+10 XP

#447 Find a Circular Import

easy

A linter checks a project for circular imports. imports is an object that maps a file name to the list of file names it imports, such as { "app.js": ["api.js"] }. A file that imports nothing may be missing from the object.

Write hasCircularImport(imports) that returns true if some file can reach itself by following imports (including a file that imports itself), and false otherwise.

Example 1

Input: imports = {"a.js":["b.js"],"b.js":["c.js"],"c.js":["a.js"]}
Output: true

a.js → b.js → c.js → a.js goes in a circle.

Example 2

Input: imports = {"app.js":["utils.js","api.js"],"api.js":["utils.js"]}
Output: false

utils.js is reached twice, but nothing leads back to a file on the current path.

Constraints

  • The object has at most 10000 keys.
  • There are at most 50000 imports in total.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).