Skip to content
meedeepdocPublic

About

Safe, zero-execution static code analyzer evaluating Big-O algorithmic time complexity and detecting hidden performance anti-patterns in Python. Built with Clean Architecture, AST NodeVisitor, FastAPI, Aiogram 3, and Unittest.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ CodeQuest: AST Algorithm Complexity Analyzer

Safe, zero-execution static code analyzer evaluating Big-O algorithmic time complexity and detecting hidden performance anti-patterns in Python code.

Python FastAPI Aiogram SQLite Tests


💡 The Problem & Product Value (Product-First)

When preparing for algorithmic technical interviews, developers frequently introduce subtle quadratic ($O(N^2)$) or exponential bottlenecks by using seemingly innocent Python methods like .count(), .index(), or .remove() inside a loop.

Traditional online judges execute user-submitted code in heavyweight Docker sandboxes, creating:

  1. Severe security risks (arbitrary code execution, fork bombs, privilege escalation).
  2. Server overhead & latency (container spin-up, CPU limits, timeouts).

CodeQuest solves this through Static Syntax Tree Analysis (Zero-Execution Sandbox):

  • 100% Safe: Code is never executed (eval() / exec() are forbidden). It is parsed into an Abstract Syntax Tree (AST) using Python's native ast module.
  • Instant Feedback (< 2ms): Syntax trees are traversed in microseconds, providing immediate Big-O bounds and architectural feedback.
  • Gamified Code Review: Evaluates code structure (e.g. modular function encapsulation vs global namespace pollution) and awards XP.

🛠️ Architecture & Clean Design

CodeQuest is built following Clean Architecture (Hexagonal / Ports & Adapters) principles:

codequest/
├── core/
│   ├── __init__.py
│   └── analyzer.py          # Pure AST Engine (ZERO external dependencies, standard library only)
├── bot/
│   ├── __init__.py
│   └── telegram_bot.py      # Aiogram 3 Async Telegram Bot Adapter
├── api/
│   ├── __init__.py
│   └── main.py              # FastAPI REST API Adapter with OpenAPI/Swagger
├── tests/
│   ├── __init__.py
│   └── test_analyzer.py     # Unittest suite covering complexity & edge cases
├── samples/                 # Sample snippets: O(1), O(N), O(N²), Anti-patterns
├── .env.example
├── .gitignore
└── requirements.txt
flowchart TD
    UserInput["Python Code (.py / snippet)"] --> EntryPoint{"Delivery Channel"}
    EntryPoint -->|Telegram Bot| BotAdapter["Telegram Bot (Aiogram 3)"]
    EntryPoint -->|HTTP / REST| APIAdapter["FastAPI Microservice"]
    EntryPoint -->|CI / CLI| CLIAdapter["CLI / Test Harness"]
    
    BotAdapter --> Core["⚡ core.analyzer (Pure AST Parser)"]
    APIAdapter --> Core
    CLIAdapter --> Core
    
    Core --> ASTVisitor["ComplexityVisitor (ast.NodeVisitor)"]
    ASTVisitor --> LoopCalc["Loop Nesting Depth: O(1), O(N), O(N²)"]
    ASTVisitor --> AntiPattern["Hidden Quadratic Calls: .count(), .index() in loops"]
    ASTVisitor --> Structure["Code Review: Function encapsulation"]
    
    LoopCalc --> Report["Structured AnalysisResult + XP Score"]
    AntiPattern --> Report
    Structure --> Report
Loading

🔬 How the AST Analyzer Works

The analyzer inherits from ast.NodeVisitor to inspect language constructs without executing code:

  1. Loop Depth Tracking:
    Tracks depth across For, AsyncFor, While, and comprehensions (ListComp, SetComp, DictComp).

    • Depth 0 $\rightarrow \mathcal{O}(1)$ (Constant)
    • Depth 1 $\rightarrow \mathcal{O}(N)$ (Linear)
    • Depth 2 $\rightarrow \mathcal{O}(N^2)$ (Quadratic)
    • Depth $k \ge 3 \rightarrow \mathcal{O}(N^k)$ (Polynomial bottleneck)
  2. Hidden Complexity Anti-Pattern Detection:
    Traverses ast.Call nodes. If a method like arr.count(x) or arr.index(x) is invoked when current_depth > 0, the engine detects a hidden $O(N)$ pass inside an outer loop, flagging the stealth $\mathcal{O}(N^2)$ penalty.

  3. Safe Error Handling:
    Captures SyntaxError with line numbers and precise descriptions without crashing the service.


🧪 Testing & Verification

The core engine is covered by automated unit tests validating all complexity tiers and error paths:

# Run unit tests (0 external dependencies required)
python3 -m unittest discover tests -v

Test Suite Results:

test_anti_pattern_hidden_complexity: detects .count() inside loops ... OK
test_missing_function_penalty: rewards clean def encapsulation ... OK
test_o1_complexity: validates constant time functions ... OK
test_on2_nested_loops: detects quadratic loop structures ... OK
test_on_complexity: validates linear algorithms ... OK
test_syntax_error_handling: captures invalid syntax gracefully ... OK
----------------------------------------------------------------------
Ran 6 tests in 0.001s (OK)

🚀 Running Locally

1. Clone & Setup Environment

git clone https://github.com/meedeepdoc/codequest.git
cd codequest

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

2. Run the REST API

uvicorn api.main:app --reload --port 8000
# Interactive API documentation will be available at http://localhost:8000/docs

3. Run the Telegram Bot

# Copy template and set your token
cp .env.example .env
# Edit .env and insert your BOT_TOKEN from @BotFather

python -m bot.telegram_bot

👨‍💻 Author

Daniil Mikhailov

  • Specialization: Frontend / Mobile Developer & Product Engineer
  • Philosophy: Zero Black Box — deep understanding of algorithms, runtime complexity, and memory management.
  • GitHub: @YaadL

About

Safe, zero-execution static code analyzer evaluating Big-O algorithmic time complexity and detecting hidden performance anti-patterns in Python. Built with Clean Architecture, AST NodeVisitor, FastAPI, Aiogram 3, and Unittest.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages