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
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json
yarn.lock

# Environment variables
.env
.env.local
.env.*.local

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Build outputs
dist/
build/
*.log

# Database
*.sqlite
*.sqlite3
*.db

# Temporary files
*.tmp
.cache/
Empty file removed src/problem1/.keep
Empty file.
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.
53 changes: 53 additions & 0 deletions src/problem4/sum_n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ===============================
// Implementation A — Iterative
// ===============================

// Complexity
// Time: O(n) — iterates from 1 to n
// Space: O(1) — constant extra memory
//
// Notes:
// - Very readable and safe
// - Slightly slower for large n due to iteration
// - Best when clarity is preferred over performance
export const sum_to_n_a = (n: number): number => {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
};

// ===============================
// Implementation B — Mathematical Formula
// ===============================

// Complexity
// Time: O(1) — constant time calculation
// Space: O(1) — constant extra memory
//
// Notes:
// - Most efficient implementation
// - No loops or recursion
// - Safe under the assumption that the result
// is less than Number.MAX_SAFE_INTEGER
export const sum_to_n_b = (n: number): number => {
return (n * (n + 1)) / 2;
};

// ===============================
// Implementation C — Recursive
// ===============================

// Complexity
// Time: O(n) — one recursive call per decrement
// Space: O(n) — call stack grows linearly
//
// Notes:
// - Elegant and expressive
// - Inefficient for large n due to recursion overhead
// - Risk of stack overflow; not recommended for production
export const sum_to_n_c = (n: number): number => {
if (n <= 1) return n;
return n + sum_to_n_c(n - 1);
};
147 changes: 147 additions & 0 deletions src/problem5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Resource Management API

A RESTful backend server built with Express.js and TypeScript that provides CRUD operations for managing resources. The application uses TypeORM with SQLite for data persistence.

## Features

- ✅ Create a resource
- ✅ List resources with pagination and filtering
- ✅ Get details of a specific resource
- ✅ Update resource details
- ✅ Delete a resource

## Tech Stack

- **Runtime**: Node.js
- **Framework**: Express.js
- **Language**: TypeScript
- **ORM**: TypeORM
- **Database**: SQLite
- **Validation**: Zod

## Prerequisites

- Node.js (v14 or higher)
- npm or yarn

## Installation

1. Install dependencies:
```bash
npm install
```

## Configuration

The application uses SQLite database which is automatically created as `database.sqlite` in the project root. No additional configuration is required for development.

The server runs on port `3000` by default. You can change this by setting the `PORT` environment variable:

```bash
export PORT=3000 # Linux/Mac
set PORT=3000 # Windows
```

## Running the Application

### Development Mode

Run the server in development mode with hot-reload:

```bash
npm run dev
```

The server will start on `http://localhost:3000` (or the port specified in the `PORT` environment variable).

### Production Mode

1. Build the TypeScript code:
```bash
npm run build
```

2. Start the server:
```bash
npm start
```

## API Endpoints

All endpoints are prefixed with `/resources`.

### Create a Resource

```http
POST /resources
Content-Type: application/json

{
"name": "My Resource",
"description": "Resource description (optional)"
}
```

**Response**: `201 Created` with the created resource

### List Resources

```http
GET /resources?page=1&limit=10&name=search
```

**Query Parameters**:
- `page` (optional): Page number (default: 1)
- `limit` (optional): Items per page (default: 10)
- `name` (optional): Filter by name

**Response**: `200 OK` with paginated list of resources

### Get Resource Details

```http
GET /resources/:id
```

**Response**: `200 OK` with resource details or `404 Not Found`

### Update a Resource

```http
PUT /resources/:id
Content-Type: application/json

{
"name": "Updated Name",
"description": "Updated description"
}
```

**Response**: `200 OK` with updated resource or `404 Not Found`

### Delete a Resource

```http
DELETE /resources/:id
```

**Response**: `204 No Content` on success or `400 Bad Request` if ID is invalid

## Project Structure

```
src/
├── app.ts # Express app configuration
├── server.ts # Server entry point
├── database.ts # TypeORM data source configuration
└── resource/
├── resource.controller.ts # Request handlers
├── resource.usecases.ts # Business logic
├── resource.repository.ts # Data access layer
├── resource.orm-entity.ts # TypeORM entity
└── resource.routes.ts # Route definitions
```

## License

ISC
29 changes: 29 additions & 0 deletions src/problem5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "problem5",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"dev": "ts-node-dev --respawn --transpile-only src/server.ts",
"build": "tsc",
"start": "node dist/server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^17.2.3",
"express": "^5.2.1",
"reflect-metadata": "^0.2.2",
"sqlite3": "^5.1.7",
"typeorm": "^0.3.28",
"zod": "^4.3.5"
},
"devDependencies": {
"@types/express": "^5.0.6",
"@types/node": "^25.0.9",
"ts-node-dev": "^2.0.0",
"typescript": "^5.9.3"
}
}
26 changes: 26 additions & 0 deletions src/problem5/src/api-test.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
### Create a Resource
POST http://localhost:3000/resources
Content-Type: application/json

{
"name": "My Test Resource",
"description": "Hello REST Client"
}

### List Resources
GET http://localhost:3000/resources?page=1&limit=5

### Get a single Resource
GET http://localhost:3000/resources/890ae7b9-9c96-406f-b64b-2b24efeeedeb

### Update a Resource
PUT http://localhost:3000/resources/890ae7b9-9c96-406f-b64b-2b24efeeedeb
Content-Type: application/json

{
"name": "Updated Resource",
"description": "Updated description"
}

### Delete a Resource
DELETE http://localhost:3000/resources/1
11 changes: 11 additions & 0 deletions src/problem5/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from "express";
import resourceRoutes from "./resource/resource.routes";
import { AppDataSource } from "./database";

export const createApp = async () => {
await AppDataSource.initialize();
const app = express();
app.use(express.json());
app.use("/resources", resourceRoutes);
return app;
};
10 changes: 10 additions & 0 deletions src/problem5/src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "reflect-metadata";
import { DataSource } from "typeorm";
import { ResourceOrmEntity } from "./resource/resource.orm-entity";

export const AppDataSource = new DataSource({
type: "sqlite",
database: "database.sqlite",
synchronize: true,
entities: [ResourceOrmEntity],
});
Loading