Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/problem4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
dist/
logs/*.jsonl
!logs/.gitkeep
3 changes: 3 additions & 0 deletions src/problem4/AUDIT-LOG-SAMPLE.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"sequence":1,"timestamp":"2026-08-03T14:45:23.605Z","traceId":"2315985c-14c5-4efc-9ed5-8bd9913fb17a","action":"SUM_TO_N","status":"SUCCESS","input":100,"result":5050,"durationMs":0,"inputHash":"ad57366865126e55649ecb23ae1d48887544976efea46a48eb5d85a6eeb4d306","outputHash":"3f95b1b8a32c2c0251dfdbc3c8a30aab6d6e680cf0ef03e8af84a65dff0c4a85","error":null,"previousHash":null,"recordHash":"2391ef1857dbea8762c44c4a9601f3e3c6e4c2e895419af0594bf8e192f65454"}
{"sequence":2,"timestamp":"2026-08-03T14:45:23.606Z","traceId":"58437f1b-deed-449b-94cf-57a4bc524145","action":"SUM_TO_N","status":"SUCCESS","input":100,"result":5050,"durationMs":0,"inputHash":"ad57366865126e55649ecb23ae1d48887544976efea46a48eb5d85a6eeb4d306","outputHash":"3f95b1b8a32c2c0251dfdbc3c8a30aab6d6e680cf0ef03e8af84a65dff0c4a85","error":null,"previousHash":"2391ef1857dbea8762c44c4a9601f3e3c6e4c2e895419af0594bf8e192f65454","recordHash":"e02ac73c838b10c50a89643236797a0a45d2027e7bdd5d10b7556b0ba0e98f47"}
{"sequence":3,"timestamp":"2026-08-03T14:45:23.606Z","traceId":"b829134f-6999-48d6-8e3c-6081ccb4cf35","action":"SUM_TO_N","status":"SUCCESS","input":100,"result":5050,"durationMs":0,"inputHash":"ad57366865126e55649ecb23ae1d48887544976efea46a48eb5d85a6eeb4d306","outputHash":"3f95b1b8a32c2c0251dfdbc3c8a30aab6d6e680cf0ef03e8af84a65dff0c4a85","error":null,"previousHash":"e02ac73c838b10c50a89643236797a0a45d2027e7bdd5d10b7556b0ba0e98f47","recordHash":"729ebcf3f9e52a1f0b6025d856ea42f6ecc45d048117c1fb39f8d2128ab6b025"}
75 changes: 75 additions & 0 deletions src/problem4/DEEP-STRESS-REPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Problem 4 — Deep stress-test report

## Result

```text
Status: STRESS_TEST_PASS
Assertions: 1,197,832
Maximum safe-result input: 134,217,727
Elapsed: 1,307 ms
Suites: 13
```

## Coverage executed

- Fixed positive, negative, zero, and known-result cases.
- Exhaustive verification of all three functions from `-12,000` through `12,000`.
- 8,000 deterministic random inputs through all three functions.
- 200,000 deterministic random inputs across the complete safe-result domain for solutions A and C.
- 8,192 values immediately below the safe-result boundary, checked with both signs.
- Exact maximum safe input executed through solution B despite its linear complexity.
- First overflowing result rejected by all three functions.
- Values surrounding powers of two.
- Recurrence and sign-symmetry metamorphic properties.
- Invalid values: `NaN`, infinities, decimals, and unsafe integers.
- 315 sequential success/failure audit records.
- 600 concurrent records written by eight Node.js processes.
- Unique `traceId` verification for concurrent records.
- Audit chain verification after concurrent writes.
- Seven tamper/corruption scenarios.
- Confirmation that audit records contain no `algorithm` field.

## Suite timings

| Suite | Assertions | Elapsed |
|---|---:|---:|
| Fixed examples and signed edges | 36 | 1 ms |
| Exhaustive all-functions range | 96,004 | 96 ms |
| Random moderate range for all functions | 32,000 | 22 ms |
| Full safe-domain random test for A and C | 600,000 | 165 ms |
| Safe-result boundary sweep | 49,154 | 129 ms |
| Result-overflow rejection | 3 | 111 ms |
| Power-of-two neighborhoods | 672 | 2 ms |
| Metamorphic identities for all functions | 18,000 | 23 ms |
| Full-domain metamorphic identities for A and C | 400,000 | 144 ms |
| Invalid runtime inputs | 18 | 1 ms |
| Sequential success and failure audit chain | 733 | 112 ms |
| Concurrent multi-process audit chain | 1,203 | 497 ms |
| Audit tamper and corruption detection | 9 | 4 ms |

## Tamper scenarios detected

1. Modified result without recomputing the record hash.
2. Broken `previousHash`.
3. Deleted middle record.
4. Reordered records.
5. Duplicated record.
6. Malformed JSON.
7. Truncated final record.

## Commands

```bat
npm test
npm run stress
```

`npm run stress` executes the deep suite. `npm run stress:quick` keeps the
smaller suite for fast local checks.

## Boundary of this test

No finite test can prove correctness for every possible runtime environment.
This suite combines exhaustive ranges, independent `BigInt` oracles,
full-domain deterministic sampling, mathematical invariants, exact boundary
execution, concurrent audit writes, and deliberate log corruption.
134 changes: 134 additions & 0 deletions src/problem4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Problem 4 — Three functions with audit log

All three challenge functions remain pure and have explanatory comments
directly above them.

Each function has a corresponding audited service wrapper:

```text
executeSumAWithAudit
executeSumBWithAudit
executeSumCWithAudit
```

The audit log does not contain an `algorithm` field.

## Run one function

```bat
cd /d D:\99\code-challenge\src\problem4
npm install

npm start -- a 100
npm start -- b 100
npm start -- c 100
```

Each command prints the result directly:

```text
5050
```

Each function call appends one record to:

```text
logs/problem4-audit.jsonl
```

After running A, B, and C, the log contains three records.

## Generate three audit records at once

```bat
npm run audit:sample -- 100
```

## Audit fields

```text
sequence
timestamp
traceId
action
status
input
result
durationMs
inputHash
outputHash
error
previousHash
recordHash
```

There is no `algorithm` field.

## Verify audit integrity

```bat
npm run audit:verify
```

Expected after the sample command:

```text
AUDIT_OK records=3
```

## Test

```bat
npm test
```

Expected result:

```text
PASS
```


## Stress test

Run correctness, boundary, invalid-input, and audit-chain stress checks:

```bat
npm run stress
```

Expected output:

```text
STRESS_TEST_PASS
assertions=...
auditRecords=300
safeBoundary=134217727
elapsedMs=...
```

The stress test does not benchmark or compare algorithms.


## Deep stress test

```bat
npm run stress
```

This runs exhaustive, random full-domain, exact-boundary, overflow,
metamorphic, invalid-input, sequential audit, concurrent multi-process audit,
and tamper-detection checks.

The verified output from the packaged run is documented in:

```text
DEEP-STRESS-REPORT.md
deep-stress-result.json
```

For a faster local check:

```bat
npm run stress:quick
```
73 changes: 73 additions & 0 deletions src/problem4/STRESS-RESULTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Problem 4 — Stress-test results

## Verification status

```text
Status: PASSED
Assertions: 1,869,698
Maximum safe-result input: 134,217,727
Node.js: v22.16.0
Version 5.8.3
```

## Test coverage

The automated stress suite verifies:

1. fixed examples, zero, and signed edge cases;
2. independent iterative accumulation for every input from `-100,000` to `100,000`;
3. 250,000 deterministic random inputs checked against a `BigInt` oracle;
4. 4,096 values at both positive and negative safe-result boundaries;
5. powers of two and neighboring values;
6. recurrence and sign metamorphic identities;
7. invalid runtime inputs;
8. warmed, multi-round performance benchmarks with rotating execution order.

## Benchmark

The benchmark uses varying valid inputs, 40,000 warm-up calls per
implementation, 9 measured rounds, and 120,000 calls per round.

| Rank | Implementation | Median | Operations/second | Relative |
|---:|---|---:|---:|---:|
| 1 | `sum_to_n_a` — Gauss formula | 1.574 ms | 76,220,723 | 1.00× |
| 2 | `sum_to_n_c` — Binary block decomposition | 24.352 ms | 4,927,689 | 15.47× |
| 3 | `sum_to_n_b` — Matrix exponentiation | 192.863 ms | 622,202 | 122.50× |

## Best algorithm

### `sum_to_n_a` — Gauss formula

It is the best implementation for this exact problem because it has:

```text
Time: O(1)
Space: O(1)
```

It also has the smallest implementation surface and the lowest measured median
runtime in this stress test.

### Why not B?

`sum_to_n_b` is algorithmically interesting and demonstrates matrix
exponentiation, but it performs many fixed-size matrix multiplications. Its
`O(log |n|)` complexity cannot beat a direct `O(1)` identity for this problem.

### Why not C?

`sum_to_n_c` is more lightweight than matrix exponentiation and demonstrates
binary decomposition, but it still inspects `O(log |n|)` blocks. It is useful
as an alternative algorithm, not as the production choice.

## Recommendation

Keep all three functions because the task explicitly requests three unique
implementations, but treat `sum_to_n_a` as the preferred implementation.

```ts
export const sum_to_n = sum_to_n_a;
```

Benchmark timing is environment-dependent. The correctness result and
asymptotic recommendation do not depend on the benchmark machine.
18 changes: 18 additions & 0 deletions src/problem4/audit-sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
executeSumAWithAudit,
executeSumBWithAudit,
executeSumCWithAudit,
} from "./service";

const input = process.argv[2] ?? "100";

const resultA =
executeSumAWithAudit(input).result;
const resultB =
executeSumBWithAudit(input).result;
const resultC =
executeSumCWithAudit(input).result;

console.log(resultA);
console.log(resultB);
console.log(resultC);
Loading