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
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "code-challenge",
"version": "1.0.0",
"description": "Note that if you fork this repository, your responses may be publicly linked to this repo. Please submit your application along with the solutions attached or linked.",
"homepage": "https://github.com/thphucle/code-challenge#readme",
"bugs": {
"url": "https://github.com/thphucle/code-challenge/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/thphucle/code-challenge.git"
},
"license": "ISC",
"author": "",
"type": "commonjs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
75 changes: 69 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,73 @@
# 99Tech Code Challenge #1 #

Note that if you fork this repository, your responses may be publicly linked to this repo.
Please submit your application along with the solutions attached or linked.
# Problem 4: Three Ways to Sum to n

It is important that you minimally attempt the problems, even if you do not arrive at a working solution.
**Location**: `src/problem4/`

## 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.
This problem implements three unique approaches to calculate the summation of integers from 1 to n:

- **Implementation A**: Recursive approach
- **Implementation B**: Iterative/Loop approach
- **Implementation C**: Mathematical formula approach

---

# Problem 5: A Crude Server

**Location**: `src/problem5/`

An Express backend with TypeScript providing RESTful CRUD operations for resource management using SQLite.

**Features**:

- CRUD operations (Create, Read, Update, Delete)
- Full TypeScript type safety
- SQLite database for persistence
- Filtering & search capabilities
- Pagination support (limit and offset)
- Comprehensive error handling
- CORS support

**Project Structure**:

```
src/
├── server.ts # Main Express server entry point
├── controllers/ # Business logic for CRUD operations
├── database/ # SQLite database configuration
├── middlewares/ # Error handling middleware
├── repositories/ # SQL command execution layer
├── routes/ # API route definitions
└── types/ # TypeScript type definitions
```

**How to run**:

1. Navigate to `src/problem5/`
2. Install dependencies: `npm install`
3. Configure environment variables (see README.md for details)
4. Build TypeScript: `npm run build` (if applicable)
5. Start the server: `npm start`
6. Test API endpoints with your preferred HTTP client (Postman, curl, etc.)

**API Endpoints**:

- `GET /resource` - Get all resources with optional filtering and pagination
- `POST /resource` - Create a new resource
- `GET /resource/:id` - Get a specific resource
- `PUT /resource/:id` - Update a resource
- `DELETE /resource/:id` - Delete a resource

---

# Problem 6: Scoreboard API Service - Architecture

**Location**: `src/problem6/`

A specification about backend service designed to manage user scores and provide real-time leaderboard updates using Redis and PostgreSQL.

**Core Components**:

- `LeaderboardService.ts` - Manages leaderboard operations
- `RedisClient.ts` - Redis connection
- `README.md` - The final specification
27 changes: 0 additions & 27 deletions src/problem2/index.html

This file was deleted.

Empty file removed src/problem2/script.js
Empty file.
8 changes: 0 additions & 8 deletions src/problem2/style.css

This file was deleted.

Empty file removed src/problem3/.keep
Empty file.
123 changes: 123 additions & 0 deletions src/problem4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Problem 4: Three Ways to Sum to n

## Problem Description

Provide 3 unique implementations of a function that calculates the summation of all integers from 1 to n.

### Specifications

- **Input**: `n` - any integer
- **Output**: summation to n (e.g., `sum_to_n(5) === 1 + 2 + 3 + 4 + 5 === 15`)
- **Assumptions**:
- Result will always be less than `Number.MAX_SAFE_INTEGER`
- Negative integers should return 0
- Non-integer inputs should be truncated to the nearest lower integer

---

## Implementation A: Recursion

```typescript
function sum_to_n_a(n: number): number {
if (n < 0)
return 0;

n = Math.trunc(n);

if (n <= 1)
return n;
else
return n + sum_to_n_a(n - 1);
}
```

### Complexity Analysis

| Metric | Value |
|-------- |-------|
| **Time Complexity** | O(n) |
| **Space Complexity** | O(n) |

### Efficiency Comment

Least efficient due to function call overhead and risk of stack overflow for large n values. However, it is elegant and easy to understand. Not recommended for production use with large inputs.

---

## Implementation B: Loop (Iterative)

```typescript
function sum_to_n_b(n: number): number {
if (n < 0)
return 0;

n = Math.trunc(n);

let sum: number = 0;

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

return sum;
}
```

### Complexity Analysis

| Metric | Value |
|-------- |-------|
| **Time Complexity** | O(n) |
| **Space Complexity** | O(1) |

### Efficiency Comment

More efficient than recursion. No stack overflow risk. Provides a good balance between readability and performance for moderate values of n. Suitable for general use cases.

---

## Implementation C: Mathematical Formula

```typescript
function sum_to_n_c(n: number): number {
if (n < 0)
return 0;

n = Math.trunc(n);

return (n * (n + 1)) / 2;
}
```

### Complexity Analysis

| Metric | Value |
|-------- |-------|
| **Time Complexity** | O(1) |
| **Space Complexity** | O(1) |

### Efficiency Comment

**Most efficient**. Uses the mathematical formula for arithmetic series: n(n+1)/2. Delivers instant results regardless of input size with no loops or recursion. Recommended for production use.

---

## Summary Comparison

| Method | Time | Space | Best For |
|-------- |------|------- |---------- |
| **Recursion** | O(n) | O(n) | Educational purposes, small n values |
| **Loop** | O(n) | O(1) | Balanced approach, moderate n values |
| **Formula** | O(1) | O(1) | **Production code, optimal solution** |

---

## Example Usage

```typescript
console.log(sum_to_n_a(5)); // Output: 15
console.log(sum_to_n_b(5)); // Output: 15
console.log(sum_to_n_c(5)); // Output: 15

console.log(sum_to_n_a(-3)); // Output: 0
console.log(sum_to_n_b(3.7)); // Output: 6 (truncates to 3)
```
6 changes: 6 additions & 0 deletions src/problem5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
dist/
*.log
.env
.DS_Store
resources.db
Loading