Code Judge (FastAPI Backend)

A lightweight Online Code Judge built using Flask, designed to replicate the core functionality of platforms like Codeforces or LeetCode. It executes user-submitted Python code, evaluates it against predefined test cases, and provides detailed verdicts. This project serves as a foundational implementation to understand the intricacies of automated code evaluation systems, offering a pocket-sized, yet powerful, solution for competitive programming practice or educational purposes.

Code Judge

Key Features

🧠 How It Works

  • 1.
    User sends Python code via API: Submissions are made through a dedicated REST API endpoint.
  • 2.
    Code is written to a temporary file: The received code is saved to a secure, temporary location on the server filesystem.
  • 3.
    Code is executed using `subprocess`: Python's `subprocess` module is leveraged to run the user's code as a separate process.
  • 4.
    Input is piped through stdin: Test case inputs are redirected to the executing code's standard input stream.
  • 5.
    Output is captured from stdout: The executed code's standard output is captured for later comparison.
  • 6.
    Output is compared with expected output: The captured output is then meticulously compared against the known correct output.
  • 7.
    Judge returns a detailed verdict: Based on the comparison and execution metrics, a final verdict is determined.
  • ⚠️ Note on Sandboxing: Current implementation provides basic process isolation primarily through timeouts. Advanced security measures like Docker containerization are considered for future enhancements.

🛠 Tech Stack

  • 🐍
    PythonThe core programming language for the backend logic and code execution.
  • 🌶
    FastAPIA lightweight web framework used to build the API for handling submissions.
  • ⚙️
    subprocess moduleEssential for spawning and managing external processes to execute user code.
  • 📄
    tempfile moduleUtilized for securely creating and managing temporary files for code storage and execution.
  • 🧪
    PostmanUsed extensively for API development, testing, and debugging the backend endpoints.

Understanding the Verdict System

🟢

AC — Accepted

Correct output for all test cases within limits.

🔴

WA — Wrong Answer

Output did not match expected output.

⚠️

RE — Runtime Error

Code terminated abnormally (crash/exception).

TLE — Time Limit

Execution exceeded maximum allowed time.

View on GitHub