Write a function. Test real cases. Improve your solution.
Given modules [{id, deps}], return {order, blocked}. ids are unique strings, and deps is an array of existing module ids that must finish before that module can build. Repeated dependency ids count once; self-dependencies are allowed. Repeatedly choose the earliest module in original input order whose dependencies are all built, append its id to order, and consider it built. Stop when no remaining module is ready. blocked contains all unbuilt ids in original input order, including modules waiting downstream of a cycle. Leave the input unchanged. Inputs contain at most 100 modules; prioritize correctness over optimization.
[
[
{
"id": "app",
"deps": [
"ui",
"api"
]
},
{
"id": "ui",
"deps": [
"core"
]
},
{
"id": "api",
"deps": [
"core"
]
},
{
"id": "core",
"deps": []
}
]
]{
"order": [
"core",
"ui",
"api",
"app"
],
"blocked": []
}[
[
{
"id": "app",
"deps": [
"core"
]
},
{
"id": "core",
"deps": []
},
{
"id": "independent",
"deps": []
}
]
]{
"order": [
"core",
"app",
"independent"
],
"blocked": []
}[
[
{
"id": "a",
"deps": [
"b"
]
},
{
"id": "b",
"deps": [
"a"
]
},
{
"id": "report",
"deps": [
"a"
]
},
{
"id": "docs",
"deps": []
}
]
]{
"order": [
"docs"
],
"blocked": [
"a",
"b",
"report"
]
}[
[
{
"id": "self",
"deps": [
"self"
]
},
{
"id": "free",
"deps": []
}
]
]{
"order": [
"free"
],
"blocked": [
"self"
]
}[
[
{
"id": "app",
"deps": [
"lib",
"lib"
]
},
{
"id": "lib",
"deps": []
}
]
]{
"order": [
"lib",
"app"
],
"blocked": []
}[
[
{
"id": "z",
"deps": []
},
{
"id": "a",
"deps": []
},
{
"id": "m",
"deps": []
}
]
]{
"order": [
"z",
"a",
"m"
],
"blocked": []
}[ [] ]
{
"order": [],
"blocked": []
}[
[
{
"id": "constructor",
"deps": [
"__proto__"
]
},
{
"id": "__proto__",
"deps": []
}
]
]{
"order": [
"__proto__",
"constructor"
],
"blocked": []
}Your code runs in your browser. These exercises use synchronous JavaScript functions and built-in data types; DOM access, network requests, and npm imports are unavailable. Runs stop after about four seconds. Test results support self-study and don’t certify proficiency.