Skip to content

Latest commit

 

History

History
 
 

README.md

LeetCode - Online Judge

Codebase Stats

Metric Value
Total SLOC 6,694
Source Files 48
.js 2,916
.md 1,577
.tsx 1,406
.ts 431
.css 109

Overview

An online coding practice and evaluation platform where users can solve programming challenges, submit code solutions, and track their progress.

Key Features

  • 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

Implementation Status

  • 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)

Getting Started

Prerequisites

  • Node.js 20+
  • Docker and Docker Compose
  • PostgreSQL 16 (via Docker)
  • Redis 7 (via Docker)

Option 1: Docker Setup (Recommended)

  1. Start infrastructure services:
cd leetcode
docker-compose up -d

This starts:

  • PostgreSQL on port 5432
  • Redis on port 6379
  1. Install backend dependencies:
cd backend
npm install
  1. Seed the database:
npm run seed

This creates:

  • Admin user: admin / admin123
  • Demo user: demo / user123
  • 15 sample problems across easy, medium, and hard difficulties
  1. Start the backend:
npm run dev

Backend runs on http://localhost:3001

  1. Install frontend dependencies (new terminal):
cd frontend
npm install
  1. Start the frontend:
npm run dev

Frontend runs on http://localhost:5173

Option 2: Native PostgreSQL/Redis

If you have PostgreSQL and Redis installed locally:

  1. Create a database:
CREATE DATABASE leetcode;
CREATE USER leetcode WITH PASSWORD 'leetcode_pass';
GRANT ALL PRIVILEGES ON DATABASE leetcode TO leetcode;
  1. Run the init script:
psql -U leetcode -d leetcode -f backend/src/db/init.sql
  1. Start Redis:
redis-server
  1. Follow steps 2-6 from Docker setup above.

Project Structure

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

API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - Login
  • POST /api/v1/auth/logout - Logout
  • GET /api/v1/auth/me - Get current user

Problems

  • GET /api/v1/problems - List all problems
  • GET /api/v1/problems/:slug - Get problem details
  • GET /api/v1/problems/:slug/submissions - Get user's submissions for a problem

Submissions

  • POST /api/v1/submissions - Submit code
  • POST /api/v1/submissions/run - Run code against sample tests
  • GET /api/v1/submissions/:id - Get submission details
  • GET /api/v1/submissions/:id/status - Poll submission status

Users

  • GET /api/v1/users/:id/profile - Get user profile
  • GET /api/v1/users/:id/submissions - Get user's submission history
  • GET /api/v1/users/me/progress - Get current user's progress

Admin

  • GET /api/v1/admin/stats - Get system statistics
  • GET /api/v1/admin/users - List all users
  • GET /api/v1/admin/leaderboard - Get leaderboard

Running Multiple Backend Instances

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 3003

Code Execution

The 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.

Technology Stack

  • 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

Architecture

See architecture.md for detailed system design documentation.

Development Notes

See claude.md for development insights and iteration history.

Implemented Features

  • 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

Kafka Queue Mode (Optional)

For production scale, enable Kafka queue-based execution:

  1. Start Kafka:
docker-compose up -d zookeeper kafka
  1. Enable queue mode:
export USE_KAFKA_QUEUE=true
  1. 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.

Future Enhancements

  • Contests with real-time leaderboards
  • Code similarity detection for plagiarism
  • WebSocket for real-time submission updates

References & Inspiration