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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Please submit your application along with the solutions attached or linked.

It is important that you minimally attempt the problems, even if you do not arrive at a working solution.

## Submission ##
## Submission
You can either provide a link to an online repository, attach the solution in your application, or whichever method you prefer.
We're cool as long as we can view your solution without any pain.
Empty file removed src/problem4/.keep
Empty file.
20 changes: 20 additions & 0 deletions src/problem4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Problem 4: Sum to N

Three ways to calculate the sum of integers from 1 to n.

## Solutions

**A. Math formula** — O(1) time, O(1) space
Uses the formula `n * (n + 1) / 2`

**B. Iterative** — O(n) time, O(1) space
Loops from 1 to n, accumulating the sum

**C. Recursive** — O(n) time, O(n) space
Calls itself with `n-1` until reaching the base case

## Test

```bash
npx tsx src/problem4/index.test.ts
```
22 changes: 22 additions & 0 deletions src/problem4/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { sum_to_n_a, sum_to_n_b, sum_to_n_c } from "./index";

function assertEquals(actual: number, expected: number, name: string) {
if (actual !== expected) {
throw new Error(`${name}: expected ${expected}, got ${actual}`);
}
console.log(`${name}: PASS`);
}

assertEquals(sum_to_n_a(5), 15, "sum_to_n_a(5)");
assertEquals(sum_to_n_a(10), 55, "sum_to_n_a(10)");
assertEquals(sum_to_n_a(0), 0, "sum_to_n_a(0)");

assertEquals(sum_to_n_b(5), 15, "sum_to_n_b(5)");
assertEquals(sum_to_n_b(10), 55, "sum_to_n_b(10)");
assertEquals(sum_to_n_b(0), 0, "sum_to_n_b(0)");

assertEquals(sum_to_n_c(5), 15, "sum_to_n_c(5)");
assertEquals(sum_to_n_c(10), 55, "sum_to_n_c(10)");
assertEquals(sum_to_n_c(0), 0, "sum_to_n_c(0)");

console.log("All tests passed!");
33 changes: 33 additions & 0 deletions src/problem4/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 1st solution
* O(1) time, O(1) space
*/
export const sum_to_n_a = (n: number): number => {
return (n * (n + 1)) / 2;
};

/**
* 2nd solution: Iterative
* O(n) time, O(1) space
*/
export const sum_to_n_b = (n: number): number => {
let sum = 0;

for (let i = 1; i <= n; i++) {
sum += i;
}

return sum;
};

/**
* 3rd solution: Recursive
* O(n) time, O(n) space
*/
export const sum_to_n_c = (n: number): number => {
if (n <= 1) {
return n;
}

return n + sum_to_n_c(n - 1);
};
3 changes: 3 additions & 0 deletions src/problem5/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGODB_URI=mongodb://localhost:27017/99problem5
PORT=3000
X_API_KEY=your-secret-api-key
Empty file removed src/problem5/.keep
Empty file.
Loading