Implement a named algorithm in your language with an explicit interface, input constraints, complexity, edge-case handling and runnable tests.
Implement the algorithm below for the stated use case. Choose a variant that fits the input constraints, write readable code in the project's style, and supply tests that a reviewer can run. ## Inputs Algorithm: [ALGORITHM] Language and version: [LANGUAGE] Use case and caller: [USE_CASE] Input constraints (sizes, value ranges, ordering, duplicates, negative values): [CONSTRAINTS] Required function or class signature: [SIGNATURE] Libraries that may or may not be used: [LIBRARY_RULES] ## Instructions 1. State which variant you are implementing and why it suits the constraints (for example, binary-heap Dijkstra versus Bellman-Ford when weights may be negative). 2. Validate or document preconditions. Reject inputs the algorithm cannot handle correctly instead of returning a wrong answer. 3. Comment only the non-obvious steps, such as invariants and termination conditions. 4. Derive time and space complexity in terms of the input variables. 5. Cover edge cases: empty input, single element, duplicates or ties, unreachable or missing values, boundary sizes, and invalid input. ## Return 1. Chosen variant and rationale. 2. Implementation matching [SIGNATURE]. 3. Complexity analysis. 4. A test file in the project's test framework covering normal, boundary and invalid inputs, with the command to run it. 5. A short usage example. 6. Alternatives and when each would be the better choice. ## Acceptance checks - The signature matches [SIGNATURE] exactly. - Each edge case listed above has either a test or a stated reason it cannot occur. - Complexity claims match the data structures used. - Do not state that tests passed unless they were run; label expected results as expected.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Algorithm: Dijkstra's shortest paths Language: TypeScript 5, Node 22 Use case: delivery routing service computing travel time from one depot Constraints: up to 50,000 nodes and 200,000 directed edges; weights are non-negative integers (seconds); graph may be disconnected Signature: shortestPaths(graph: Map<string, Array<[string, number]>>, source: string): Map<string, number> Libraries: no third-party packages
An implementation with a hand-written binary min-heap (no third-party packages), a guard that throws on a negative weight or unknown source, unreachable nodes reported as Infinity, complexity O((V + E) log V) time and O(V) auxiliary space, and tests for a disconnected node, a single-node graph, zero-weight edges, two equal-cost paths and a negative weight. Illustrative expectation, not a recorded model run.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
Models