Implement prototype(weights, target) and solve(weights, target). Return the lexicographically smallest ascending array [a, b] with a + b === target, drawn from distinct positions; return null if absent. Negatives and duplicates are valid.

[4, 1, 3, 2] with target 5 returns [1, 4]; [5] with target 10 returns null; [5, 5] returns [5, 5].

Prototype: inspect all pairs. Final: sort a copy and use inward pointers, O(n log n) time/O(n) auxiliary storage. Preserve input. No additional errors are required.

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.