ProblemService is the catalog service for the platform. It stores LeetCode-style problem statements, difficulty, optional editorial content, and the test cases used later by the evaluation pipeline.
- Expose CRUD APIs for coding problems
- Store problem metadata and test cases in MongoDB
- Sanitize markdown-based
descriptionandeditorialfields before persistence - Provide problem details to
SubmissionServiceduring submission intake
- Node.js
- TypeScript
- Express 5
- MongoDB with Mongoose
- Zod for request validation
- Winston with daily rotating log files
The service loads environment variables from .env.
| Variable | Default | Purpose |
|---|---|---|
PORT |
3000 |
HTTP port |
DB_URL |
mongodb://localhost:27017/lc_problem_db |
MongoDB connection string |
- Install dependencies:
npm install- Create
.env:
PORT=3000
DB_URL=mongodb://localhost:27017/lc_problem_db- Start the service:
npm run devThe server connects to MongoDB on startup.
Base path: /api/v1
GET /pingGET /ping/health
POST /problemsGET /problemsGET /problems/:idPUT /problems/:idDELETE /problems/:idGET /problems/difficulty/:difficultyGET /problems/search/:query
{
"title": "Two Sum",
"description": "Given an array of integers...",
"difficulty": "easy",
"editorial": "Use a hash map.",
"testcases": [
{
"input": "4\n2 7 11 15\n9",
"output": "0 1"
}
]
}Each problem document contains:
titledescriptiondifficulty:easy | medium | hardeditorialtestcases[]createdAtupdatedAt
Indexes defined in code:
- Unique index on
title - Secondary index on
difficulty
- Express receives JSON requests.
- A correlation ID is attached with
AsyncLocalStorage. - Zod validates request bodies or params where middleware is applied.
ProblemServicesanitizes markdown content.ProblemRepositorypersists and fetches data through Mongoose.- Errors are returned through shared error middleware.
Before saving a problem:
- Markdown is converted to HTML with
marked - HTML is sanitized with
sanitize-html - Sanitized HTML is converted back to markdown with
turndown
This reduces the chance of persisting unsafe HTML in descriptions or editorials.
Logs go to:
- Console
logs/%DATE%-app.log
Each log entry includes a correlation ID where available.
GET /problems/search/:queryis registered with a path parameter, but the controller currently readsreq.query.q. As written, searches will not use:queryunless that controller is corrected.POST /problemsallowstestcasesto be optional at validation layer, but the Mongoose schema requires them. Invalid payloads will fail at persistence time rather than validation time.PUT /problems/:idis not currently protected by a request-body validator./api/v2exists but has no routes yet.