Skip to content

rafaelcapeloo/SolveTrace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ SolveTrace

Open-source competitive programming problem analyzer
Paste a URL β†’ Get a step-by-step solution with complexity analysis, code, and interview tips.

License Python Next.js FastAPI


πŸ“‹ Table of Contents


🎯 What It Does

SolveTrace takes a competitive programming problem (via URL or pasted text) and generates a comprehensive analysis:

  1. Auto-scrapes the problem statement, examples, input/output constraints
  2. Queries a RAG knowledge base of 30+ algorithm patterns for relevant techniques
  3. Sends everything to your chosen LLM with a carefully crafted prompt
  4. Returns a structured breakdown:
    • πŸ”’ Step-by-step approach with reasoning
    • ⏱️ Time & space complexity with explanations
    • πŸ’» Clean, well-commented code solution
    • πŸ’‘ Key insights & common pitfalls
    • πŸ”— Related problems from other platforms
    • πŸŽ“ Interview tips

🌐 Supported Platforms

Platform Scraping Example URL
Codeforces βœ… Full support codeforces.com/problemset/problem/1/A
LeetCode βœ… Full support leetcode.com/problems/two-sum
AtCoder βœ… Full support atcoder.jp/contests/abc001/tasks/abc001_1
HackerRank βœ… Full support hackerrank.com/challenges/solve-me-first

You can also paste problem text directly if your platform isn't supported.

🧠 Choose Your LLM

SolveTrace supports 3 LLM providers β€” pick the one that fits your needs:

Provider Type Cost Setup Best For
Ollama Local Free Install Ollama + pull a model Privacy, offline use, no API costs
Google Gemini Cloud API Free tier available Get API key from Google AI Studio Fast, generous free tier
OpenAI Cloud API Paid Get API key from OpenAI GPT-4o quality

Ollama (Recommended for Getting Started)

Ollama lets you run AI models locally on your machine β€” completely free, no API key needed.

# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh   # Linux/macOS
# Windows: download from https://ollama.com/download

# Pull a coding-optimized model (recommended)
ollama pull qwen2.5-coder:7b

# Or use a larger model for better quality
ollama pull qwen2.5-coder:14b
ollama pull llama3.1:8b
ollama pull codellama:13b

Google Gemini

  1. Go to Google AI Studio
  2. Create an API key (free tier: 15 requests/minute)
  3. Set GEMINI_API_KEY=your_key_here in backend/.env
  4. Set LLM_PROVIDER=gemini in backend/.env

OpenAI

  1. Go to OpenAI API Keys
  2. Create an API key (requires billing setup)
  3. Set OPENAI_API_KEY=your_key_here in backend/.env
  4. Set LLM_PROVIDER=openai in backend/.env

πŸš€ Quick Start

Prerequisites

Software Version Download
Python 3.11+ python.org
Node.js 18+ nodejs.org
Git Any git-scm.com
Ollama (or API key) Latest ollama.com

Setup on Linux / macOS

# 1. Clone the repository
git clone https://github.com/rafaelcapeloo/SolveTrace.git
cd SolveTrace

# 2. Setup Backend
cd backend
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

# 3. Configure your LLM provider
cp .env.example .env
nano .env   # or use your preferred editor
# β†’ Set LLM_PROVIDER to "ollama", "gemini", or "openai"
# β†’ Set the corresponding API key if using a cloud provider

# 4. Start the backend server
uvicorn app.main:app --reload --port 8000

# 5. Open a new terminal tab, setup Frontend
cd ../frontend
npm install
npm run dev

Open http://localhost:3000 and start analyzing! πŸŽ‰

Setup on Windows

# 1. Clone the repository
git clone https://github.com/rafaelcapeloo/SolveTrace.git
cd SolveTrace

# 2. Setup Backend
cd backend
python -m venv venv
.\venv\Scripts\activate
pip install -r requirements.txt

# 3. Configure your LLM provider
copy .env.example .env
notepad .env
# β†’ Set LLM_PROVIDER to "ollama", "gemini", or "openai"
# β†’ Set the corresponding API key if using a cloud provider

# 4. Start the backend server
uvicorn app.main:app --reload --port 8000

# 5. Open a new terminal window, setup Frontend
cd ..\frontend
npm install
npm run dev

Open http://localhost:3000 and start analyzing! πŸŽ‰

Note for Windows users: If uvicorn is not recognized, try python -m uvicorn app.main:app --reload --port 8000


βš™οΈ Configuration

All configuration is done through the backend/.env file. Here's a complete reference:

# ===========================
# LLM Provider Configuration
# ===========================

# Which LLM to use: "ollama" (local), "gemini" (Google), or "openai"
LLM_PROVIDER=ollama

# --- Ollama (Local) ---
# Make sure Ollama is running: ollama serve
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=qwen2.5-coder:7b

# --- Google Gemini ---
# Get your key: https://aistudio.google.com/apikey
GEMINI_API_KEY=
GEMINI_MODEL=gemini-2.0-flash

# --- OpenAI ---
# Get your key: https://platform.openai.com/api-keys
OPENAI_API_KEY=
OPENAI_MODEL=gpt-4o-mini

# ===========================
# Server Configuration
# ===========================

# SQLite database for caching scraped problems and analyses
DATABASE_URL=sqlite+aiosqlite:///./solvetrace.db

# Frontend URL (for CORS)
CORS_ORIGINS=http://localhost:3000,http://localhost:5173

# Set to true for verbose SQL and debug logging
DEBUG=false

Switching LLM Providers

You can switch providers at any time by changing the LLM_PROVIDER value and restarting the backend. You can also switch providers from the UI using the LLM Provider dropdown on the Solve page β€” providers with a βœ“ are configured and ready, while providers with ⚠ need their API key set in .env.


πŸ“ Project Structure

SolveTrace/
β”œβ”€β”€ backend/                          # FastAPI Python backend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”‚   β”œβ”€β”€ analysis.py           # POST /api/analyze β€” main analysis endpoint
β”‚   β”‚   β”‚   └── problems.py           # GET /api/problems β€” browse cached problems
β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   β”œβ”€β”€ analysis.py           # Analysis SQLAlchemy model (stores LLM results)
β”‚   β”‚   β”‚   └── problem.py            # Problem model (caches scraped data)
β”‚   β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”‚   └── analysis.py           # Pydantic request/response schemas
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   β”œβ”€β”€ analyzer.py           # 🧠 Core pipeline: Scrape β†’ RAG β†’ LLM β†’ Store
β”‚   β”‚   β”‚   β”œβ”€β”€ llm/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ gemini.py         # Google Gemini API client
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ollama.py         # Local Ollama client
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ openai_client.py  # OpenAI GPT client
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ parser.py         # JSON response parser + validator
β”‚   β”‚   β”‚   β”‚   └── prompts.py        # LLM prompt templates
β”‚   β”‚   β”‚   β”œβ”€β”€ rag/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ knowledge.py      # 30+ algorithm pattern definitions
β”‚   β”‚   β”‚   β”‚   └── vectorstore.py    # Lightweight vector store (Gemini embeddings / keyword fallback)
β”‚   β”‚   β”‚   └── scraper/
β”‚   β”‚   β”‚       β”œβ”€β”€ base.py           # Base scraper interface
β”‚   β”‚   β”‚       β”œβ”€β”€ codeforces.py     # Codeforces scraper
β”‚   β”‚   β”‚       β”œβ”€β”€ leetcode.py       # LeetCode scraper
β”‚   β”‚   β”‚       β”œβ”€β”€ atcoder.py        # AtCoder scraper
β”‚   β”‚   β”‚       └── hackerrank.py     # HackerRank scraper
β”‚   β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”‚   └── helpers.py            # URL parsing, language validation
β”‚   β”‚   β”œβ”€β”€ config.py                 # Settings from environment variables
β”‚   β”‚   β”œβ”€β”€ database.py               # Async SQLAlchemy + SQLite setup
β”‚   β”‚   └── main.py                   # FastAPI app entry point
β”‚   β”œβ”€β”€ .env.example                  # Configuration template
β”‚   └── requirements.txt              # Python dependencies
β”‚
β”œβ”€β”€ frontend/                         # Next.js React frontend
β”‚   └── src/
β”‚       β”œβ”€β”€ app/
β”‚       β”‚   β”œβ”€β”€ page.tsx              # Landing page
β”‚       β”‚   β”œβ”€β”€ page.module.css       # Landing styles
β”‚       β”‚   └── solve/
β”‚       β”‚       β”œβ”€β”€ page.tsx          # 🎯 Main solve page (URL input, results, code)
β”‚       β”‚       └── page.module.css   # Solve page styles
β”‚       β”œβ”€β”€ components/
β”‚       β”‚   └── layout/
β”‚       β”‚       β”œβ”€β”€ Navbar.tsx        # Navigation bar
β”‚       β”‚       └── Navbar.module.css # Navbar styles
β”‚       β”œβ”€β”€ services/
β”‚       β”‚   └── api.ts               # API client (fetch wrapper)
β”‚       └── types/
β”‚           └── index.ts             # TypeScript interfaces
β”‚
└── README.md                         # This file

πŸ”„ How the Analysis Pipeline Works

User Input (URL or text)
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1. SCRAPE       β”‚  Auto-detect platform β†’ scrape title, statement,
β”‚     or PARSE     β”‚  examples, constraints, tags, difficulty
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  2. CACHE CHECK  β”‚  Check SQLite DB β€” if this problem was already
β”‚                  β”‚  analyzed in the same language, return cached result
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ (cache miss)
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  3. RAG QUERY    β”‚  Query 30+ algorithm patterns (DP, graphs, greedy...)
β”‚                  β”‚  using embeddings or keyword matching
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  4. LLM CALL     β”‚  Build structured prompt with problem + RAG context
β”‚                  β”‚  β†’ Send to Ollama / Gemini / OpenAI
β”‚                  β”‚  β†’ Parse JSON response
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  5. STORE & RETURNβ”‚  Save analysis to SQLite β†’ return structured result
β”‚                  β”‚  with approach, complexity, code, insights, tips
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‘ API Reference

POST /api/analyze

Analyze a competitive programming problem.

Request Body:

{
  "url": "https://codeforces.com/problemset/problem/1/A",
  "problem_text": null,
  "language": "python",
  "provider": "ollama",
  "model": null
}
Field Type Required Description
url string One of url/problem_text Problem URL
problem_text string One of url/problem_text Raw problem text
language string No (default: python) Solution language
provider string No Override LLM provider
model string No Override model name

Response: Full analysis with approach steps, complexity, code, insights, tips.

GET /api/analyze/{id}

Retrieve a previously generated analysis by ID.

GET /api/config

Get available LLM providers and their configuration status.

GET /api/problems

List cached scraped problems. Optional ?platform= filter.

GET /api/health

Health check β€” returns status and current LLM provider.


πŸ›  Tech Stack

Layer Technology Purpose
Frontend Next.js 14+, TypeScript UI, routing, SSR
Styling CSS Modules Scoped component styles
Syntax Highlighting highlight.js Code block rendering
Backend FastAPI, Python 3.11+ API, analysis pipeline
Database SQLAlchemy (async) + SQLite Cache & persistence
Scraping httpx + BeautifulSoup4 Platform-specific scrapers
RAG Custom vector store Algorithm pattern matching
LLM (Local) Ollama Run models locally
LLM (Cloud) Google Gemini / OpenAI Cloud API models

🀝 Contributing

Contributions are welcome! Here are some ideas:

Easy

  • Add more algorithm patterns to backend/app/services/rag/knowledge.py
  • Improve LLM prompts in backend/app/services/llm/prompts.py
  • Add more supported languages

Medium

  • Add scrapers for more platforms (SPOJ, UVa, DMOJ, Kattis)
  • Add localStorage-based history in the frontend
  • Add problem comparison (side-by-side analysis)
  • Add export to PDF/Markdown

Advanced

  • Add more LLM providers (Anthropic Claude, Mistral, Groq)
  • Add WebSocket streaming for real-time analysis progress
  • Add a VS Code extension

How to Contribute

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Make your changes
  4. Test locally (both frontend and backend)
  5. Submit a pull request

πŸ”§ Troubleshooting

Backend won't start

ModuleNotFoundError: No module named 'app'

Fix: Make sure you're running from the backend/ directory and your virtual environment is activated.

Ollama connection refused

httpx.ConnectError: [Errno 111] Connection refused

Fix: Make sure Ollama is running. Start it with:

ollama serve          # Start the Ollama server
ollama pull qwen2.5-coder:7b   # Pull a model if not done yet

Gemini / OpenAI API errors

ValueError: LLM analysis failed (Gemini): 403 ...

Fix: Check that your API key is correct in backend/.env and that you have sufficient quota/credits.

Frontend can't connect to backend

TypeError: Failed to fetch

Fix: Make sure the backend is running on port 8000 and that CORS_ORIGINS in .env includes your frontend URL (http://localhost:3000).

LeetCode scraping fails

LeetCode uses client-side rendering, which makes scraping harder. If scraping fails, try pasting the problem text directly using the "Paste Problem" mode.


πŸ“„ License

MIT β€” use it however you want.


Built with β˜• by @rafaelcapeloo

About

Open-source competitive programming problem analyzer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors