| Metric | Value |
|---|---|
| Total SLOC | 6,694 |
| Source Files | 48 |
| .js | 2,916 |
| .md | 1,577 |
| .tsx | 1,406 |
| .ts | 431 |
| .css | 109 |
An online coding practice and evaluation platform where users can solve programming challenges, submit code solutions, and track their progress.
- Problem catalog with descriptions, examples, and constraints
- Code editor with syntax highlighting (Python and JavaScript)
- Sandboxed code execution using Docker containers
- Test case validation with real-time results
- User progress tracking and submission history
- Admin dashboard with system statistics
- Leaderboards
- Initial architecture design
- Core functionality implementation
- Database/Storage layer (PostgreSQL + Redis)
- API endpoints
- Code execution sandbox
- Rate limiting and abuse prevention
- Circuit breaker for resilience
- Metrics and observability
- Idempotency for submissions
- Virtualized problem list
- Documentation
- Testing (vitest)
- Node.js 20+
- Docker and Docker Compose
- PostgreSQL 16 (via Docker)
- Redis 7 (via Docker)
- Start infrastructure services:
cd leetcode
docker-compose up -dThis starts:
- PostgreSQL on port 5432
- Redis on port 6379
- Install backend dependencies:
cd backend
npm install- Seed the database:
npm run seedThis creates:
- Admin user:
admin/admin123 - Demo user:
demo/user123 - 15 sample problems across easy, medium, and hard difficulties
- Start the backend:
npm run devBackend runs on http://localhost:3001
- Install frontend dependencies (new terminal):
cd frontend
npm install- Start the frontend:
npm run devFrontend runs on http://localhost:5173
If you have PostgreSQL and Redis installed locally:
- Create a database:
CREATE DATABASE leetcode;
CREATE USER leetcode WITH PASSWORD 'leetcode_pass';
GRANT ALL PRIVILEGES ON DATABASE leetcode TO leetcode;- Run the init script:
psql -U leetcode -d leetcode -f backend/src/db/init.sql- Start Redis:
redis-server- Follow steps 2-6 from Docker setup above.
leetcode/
├── backend/
│ ├── src/
│ │ ├── db/ # Database pool, redis, init.sql, seed
│ │ ├── routes/ # API routes (auth, problems, submissions, etc.)
│ │ ├── services/ # Code executor service
│ │ ├── middleware/ # Auth middleware
│ │ └── index.js # Express server entry point
│ └── package.json
├── frontend/
│ ├── src/
│ │ ├── components/ # Reusable UI components
│ │ ├── pages/ # Page components
│ │ ├── stores/ # Zustand state stores
│ │ ├── services/ # API client
│ │ └── types/ # TypeScript definitions
│ └── package.json
├── sandbox/ # Docker sandbox for code execution
├── docker-compose.yml # Infrastructure services
└── README.md
POST /api/v1/auth/register- Register new userPOST /api/v1/auth/login- LoginPOST /api/v1/auth/logout- LogoutGET /api/v1/auth/me- Get current user
GET /api/v1/problems- List all problemsGET /api/v1/problems/:slug- Get problem detailsGET /api/v1/problems/:slug/submissions- Get user's submissions for a problem
POST /api/v1/submissions- Submit codePOST /api/v1/submissions/run- Run code against sample testsGET /api/v1/submissions/:id- Get submission detailsGET /api/v1/submissions/:id/status- Poll submission status
GET /api/v1/users/:id/profile- Get user profileGET /api/v1/users/:id/submissions- Get user's submission historyGET /api/v1/users/me/progress- Get current user's progress
GET /api/v1/admin/stats- Get system statisticsGET /api/v1/admin/users- List all usersGET /api/v1/admin/leaderboard- Get leaderboard
For distributed testing:
# Terminal 1
npm run dev:server1 # Port 3001
# Terminal 2
npm run dev:server2 # Port 3002
# Terminal 3
npm run dev:server3 # Port 3003The code execution sandbox uses Docker to safely run user-submitted code:
- Security: Containers run with dropped capabilities, no network access, read-only filesystem
- Resource limits: Memory (256MB/512MB), CPU (50%), PIDs (50), time limits
- Languages: Python 3.11, JavaScript (Node 20), C++ (GCC 13), Java (OpenJDK 21)
To enable code execution, ensure Docker is running and the user has Docker socket access.
- Frontend: React 19, TypeScript, Vite, Tailwind CSS, Zustand, React Router, CodeMirror
- Backend: Node.js, Express
- Database: PostgreSQL 16
- Cache/Sessions: Redis 7
- Code Execution: Docker with security restrictions
See architecture.md for detailed system design documentation.
See claude.md for development insights and iteration history.
- Rate Limiting: Protects execution resources
- 10 submissions/minute per user
- 30 code runs/minute per user
- Brute force protection on auth endpoints
- Circuit Breaker: Graceful degradation when Docker is unavailable
- Idempotency: Prevents duplicate submissions on retry
- Metrics: Prometheus-compatible observability
- Virtualized Lists: Efficient rendering for large problem catalogs
For production scale, enable Kafka queue-based execution:
- Start Kafka:
docker-compose up -d zookeeper kafka- Enable queue mode:
export USE_KAFKA_QUEUE=true- Start workers:
npm run dev:worker1 # Worker 1
npm run dev:worker2 # Worker 2 (in another terminal)Workers consume submission jobs from Kafka and execute them independently, enabling horizontal scaling.
- Contests with real-time leaderboards
- Code similarity detection for plagiarism
- WebSocket for real-time submission updates
- Codeforces Architecture - Mike Mirzayanov on building competitive programming platforms
- Docker Security Best Practices - Container isolation for code execution
- gVisor: Container Runtime Sandbox - Google's application kernel for sandboxing
- Firecracker MicroVMs - AWS's lightweight virtualization for serverless
- MOSS (Measure of Software Similarity) - Stanford's plagiarism detection system
- Judge0 - Open Source Online Judge - Production-ready code execution system
- HackerRank Engineering Blog - Insights on online assessment platforms
- Seccomp and Linux Security Modules - System call filtering for sandboxing
- Building a Code Execution Engine - Real-world implementation insights
- Competitive Programming Handbook - Understanding competitive programming problem design