Safe, zero-execution static code analyzer evaluating Big-O algorithmic time complexity and detecting hidden performance anti-patterns in Python code.
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:
- Severe security risks (arbitrary code execution, fork bombs, privilege escalation).
- 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 nativeastmodule. - 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.
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
The analyzer inherits from ast.NodeVisitor to inspect language constructs without executing code:
-
Loop Depth Tracking:
Tracks depth acrossFor,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)
- Depth 0
-
Hidden Complexity Anti-Pattern Detection:
Traversesast.Callnodes. If a method likearr.count(x)orarr.index(x)is invoked whencurrent_depth > 0, the engine detects a hidden$O(N)$ pass inside an outer loop, flagging the stealth$\mathcal{O}(N^2)$ penalty. -
Safe Error Handling:
CapturesSyntaxErrorwith line numbers and precise descriptions without crashing the service.
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 -vtest_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)
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.txtuvicorn api.main:app --reload --port 8000
# Interactive API documentation will be available at http://localhost:8000/docs# Copy template and set your token
cp .env.example .env
# Edit .env and insert your BOT_TOKEN from @BotFather
python -m bot.telegram_botDaniil Mikhailov
- Specialization: Frontend / Mobile Developer & Product Engineer
- Philosophy: Zero Black Box — deep understanding of algorithms, runtime complexity, and memory management.
- GitHub: @YaadL