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
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Problem 4 (Node.js/TypeScript)
node_modules/
dist/
.env
.env.*
!.env.example

# Logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Optional TypeScript incremental cache
*.tsbuildinfo

migrations/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
All answers and implementations for Problems 4, 5, and 6 related to the Backend Engineer (Remote) role are documented here.
29 changes: 29 additions & 0 deletions problem_4/sum_to_n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sum_to_n_a = sum_to_n_a;
exports.sum_to_n_b = sum_to_n_b;
exports.sum_to_n_c = sum_to_n_c;
function sum_to_n_a(n) {
//time: O(n), space: O(1)
if (n <= 0)
return 0;
var sum = 0;
for (var i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
function sum_to_n_b(n) {
//time: O(n), space: O(n)
if (n <= 0)
return 0;
if (n === 1)
return 1;
return n + sum_to_n_b(n - 1);
}
function sum_to_n_c(n) {
//time: O(1), space: O(1)
if (n <= 0)
return 0;
return (n * (n + 1)) / 2;
}
23 changes: 23 additions & 0 deletions problem_4/sum_to_n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function sum_to_n_a(n: number): number {
//time: O(n), space: O(1)
if (n <= 0) return 0;
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

export function sum_to_n_b(n: number): number {
//time: O(n), space: O(n)
if (n <= 0) return 0;
if (n === 1) return 1;
return n + sum_to_n_b(n - 1);
}

export function sum_to_n_c(n: number): number {
//time: O(1), space: O(1)
if (n <= 0) return 0;
return (n * (n + 1)) / 2;
}

25 changes: 25 additions & 0 deletions problem_4/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Task

Provide 3 unique implementations of the following function in TypeScript.

- Comment on the complexity or efficiency of each function.

**Input**: `n` - any integer

*Assuming this input will always produce a result lesser than `Number.MAX_SAFE_INTEGER`*.

**Output**: `return` - summation to `n`, i.e. `sum_to_n(5) === 1 + 2 + 3 + 4 + 5 === 15`.

```go
func sum_to_n_a(n: number): number {
// your code here
}

func sum_to_n_b(n: number): number {
// your code here
}

func sum_to_n_c(n: number): number {
// your code here
}
```
25 changes: 25 additions & 0 deletions problem_4/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { sum_to_n_a, sum_to_n_b, sum_to_n_c } = require('./sum_to_n');

function expected(n) {
if (n <= 0) return 0;
return (n * (n + 1)) / 2;
}

const cases = [-3, 0, 1, 2, 5, 10, 100];
let allPass = true;
for (const n of cases) {
const a = sum_to_n_a(n);
const b = sum_to_n_b(n);
const c = sum_to_n_c(n);
const exp = expected(n);
const pass = (a === exp) && (b === exp) && (c === exp);
console.log(`n=${n}: a=${a}, b=${b}, c=${c}, expected=${exp} -> ${pass ? 'PASS' : 'FAIL'}`);
if (!pass) allPass = false;
}

if (!allPass) {
console.error('Some tests failed');
process.exit(1);
} else {
console.log('All tests passed');
}
2 changes: 2 additions & 0 deletions problem_5/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=3000
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/resources_db?schema=public"
Loading