Implement prototype(n, edges, source) and solve(n, edges, source). Vertices are 0..n-1, directed edges [u, v, weight] may have negative integer weights. Return minimum costs in vertex order, null for unreachable.

For n=3, edges [[0, 1, 4], [0, 2, 5], [2, 1, -3]], source 0, return [0, 2, 5]. Throw RangeError for n<=0, invalid source/endpoints, or a source-reachable negative cycle. Unreachable negative cycles do not throw.

Prototype: enumerate simple paths and inspect reachable cycles separately. Final: repeated relaxation and an extra detection pass. Preserve inputs on failure too; all finite returned costs are Numbers.

All numeric inputs and finite arithmetic results in this lesson are safe-integer Numbers. Use null for documented absence, not undefined or a numeric sentinel. Return actual booleans where the contract requires them. Do not add validation requirements beyond the documented inputs.

Run with Node.js 20+ from this directory:

Shell
node main.mjs --stage prototype
node main.mjs --stage final

Edit exercise.mjs; keep main.mjs unchanged. Both stages begin as unfinished learner functions. The website uses the same test functions in a separate browser worker. Tests check deterministic examples, boundaries, documented validation, input preservation, and stage-specific cases. Passing tests does not prove the required time complexity.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
No results yet.

Complete the starter code, then run the checks.
Results and errors will appear here.