Skip to main content

Command Palette

Search for a command to run...

Leetcode clone

Published
5 min read

Introduction

  • In this Blog we will Understand how will we create a problem statement with judge0 for our leetcode clone

Algorithim for create Problem -

  • get all the problem related data from the req.body

  • get the user from the cookies and check if the user is admin or not

  • add checks if no user or user is not empty then resitrict them from creating a problem

  • Now we will loop through each refrence Solutions and get the language and the solution code

  • Now with the help of judge0 we will get the language id.

  • Check if No Language ID then simply return

  • Now make a submission bach for each of the test cases-with sourcecode,language_id,stdin,expected_output

  • Now submit the batch to judge0 and it will give a token for the submission batch

  • concatinate all the tokens together

  • submit all the tokens and then judge0 will return a submission dataset

    • in Submission make a infinite loop and call judge0 api for a submission result

    • if the status are in queue and processing wait for the result

    • if the status is not in queue or in processing return from the loop

  • check for the result if not accepted the return not accepted or else

  • save the problem in the database

  • return a response to the user with the saved problem

Detail Breakdown and Structure of code and Implementation

  • Create a problem schema

  • Get the Problem Imputs

    • Check the User role again for Security

      • The Refrence-solution json input looks like

          "refrenceSolutions": {
                  "JAVASCRIPT": "const fs = require('fs');\n\n// Reading input from stdin (using fs to read all input)\n
                  const input = fs.readFileSync(0, 'utf-8').trim();\nconst [a, b] = input.split(' ').map(Number);\n\nconsole.log(a + b);",
        
                  "PYTHON": "import sys\ninput_line = sys.stdin.read()\na, b = map(int, input_line.split())\nprint(a + b)",
        
                  "JAVA": "import java.util.Scanner;\n\npublic class Main {\n public static void main(String[] args) {\n    Scanner sc = new Scanner(System.in);\n        int a = sc.nextInt();\n        int b = sc.nextInt();\n        System.out.println(a + b);\n    }\n}"
              }
        
      • From this JSON input we will destructure the language and the solution code

      • We will loop through the refrenceSolutions and for languageId we will get the language id for each language

        for other languages language ID

        http://localhost:2358/docs#statuses-and-languages-language-get-1

          [
            {
              "id": 45,
              "name": "Assembly (NASM 2.14.02)"
            },
            {
              "id": 46,
              "name": "Bash (5.0.0)"
            },
            {
              "id": 47,
              "name": "Basic (FBC 1.07.1)"
            },
            {
              "id": 48,
              "name": "C (GCC 7.4.0)"
            },
            {
              "id": 52,
              "name": "C++ (GCC 7.4.0)"
            },
            {
              "id": 49,
              "name": "C (GCC 8.3.0)"
            },
            {
              "id": 53,
              "name": "C++ (GCC 8.3.0)"
            },
            {
              "id": 50,
              "name": "C (GCC 9.2.0)"
            },
            {
              "id": 54,
              "name": "C++ (GCC 9.2.0)"
            },
            {
              "id": 51,
              "name": "C# (Mono 6.6.0.161)"
            },
            {
              "id": 55,
              "name": "Common Lisp (SBCL 2.0.0)"
            },
            {
              "id": 56,
              "name": "D (DMD 2.089.1)"
            },
            {
              "id": 57,
              "name": "Elixir (1.9.4)"
            },
            {
              "id": 58,
              "name": "Erlang (OTP 22.2)"
            },
            {
              "id": 44,
              "name": "Executable"
            },
            {
              "id": 59,
              "name": "Fortran (GFortran 9.2.0)"
            },
            {
              "id": 60,
              "name": "Go (1.13.5)"
            },
            {
              "id": 61,
              "name": "Haskell (GHC 8.8.1)"
            },
            {
              "id": 62,
              "name": "Java (OpenJDK 13.0.1)"
            },
            {
              "id": 63,
              "name": "JavaScript (Node.js 12.14.0)"
            },
            {
              "id": 64,
              "name": "Lua (5.3.5)"
            },
            {
              "id": 65,
              "name": "OCaml (4.09.0)"
            },
            {
              "id": 66,
              "name": "Octave (5.1.0)"
            },
            {
              "id": 67,
              "name": "Pascal (FPC 3.0.4)"
            },
            {
              "id": 68,
              "name": "PHP (7.4.1)"
            },
            {
              "id": 43,
              "name": "Plain Text"
            },
            {
              "id": 69,
              "name": "Prolog (GNU Prolog 1.4.5)"
            },
            {
              "id": 70,
              "name": "Python (2.7.17)"
            },
            {
              "id": 71,
              "name": "Python (3.8.1)"
            },
            {
              "id": 72,
              "name": "Ruby (2.7.0)"
            },
            {
              "id": 73,
              "name": "Rust (1.40.0)"
            },
            {
              "id": 74,
              "name": "TypeScript (3.7.4)"
            }
          ]
        
        • Check if The Language is supported or not

        • Make a submissions to send it to Judge0- for each testCases we are making an submission

          Docs : http://localhost:2358/docs#submissions-submission-batch-post

          • test cases JSON

              "testcases": [
                      {
                          "input": "100 200",
                          "output": "300"
                      },
                      {
                          "input": "-500 -600",
                          "output": "-1100"
                      },
                      {
                          "input": "0 0",
                          "output": "0"
                      }
                  ],
            

  • Now Submit the Batch all together to Judge0

    as a return you will get TOKEN from the submission Batch

  • Concatinate all the tokens together

  • Submit the tokens to Judge0 to get a response

    • Check for the status - don’t return until its in Queue or in Processing

  • Status id for the submissions

      [
        {
          "id": 1,
          "description": "In Queue"
        },
        {
          "id": 2,
          "description": "Processing"
        },
        {
          "id": 3,
          "description": "Accepted"
        },
        {
          "id": 4,
          "description": "Wrong Answer"
        },
        {
          "id": 5,
          "description": "Time Limit Exceeded"
        },
        {
          "id": 6,
          "description": "Compilation Error"
        },
        {
          "id": 7,
          "description": "Runtime Error (SIGSEGV)"
        },
        {
          "id": 8,
          "description": "Runtime Error (SIGXFSZ)"
        },
        {
          "id": 9,
          "description": "Runtime Error (SIGFPE)"
        },
        {
          "id": 10,
          "description": "Runtime Error (SIGABRT)"
        },
        {
          "id": 11,
          "description": "Runtime Error (NZEC)"
        },
        {
          "id": 12,
          "description": "Runtime Error (Other)"
        },
        {
          "id": 13,
          "description": "Internal Error"
        },
        {
          "id": 14,
          "description": "Exec Format Error"
        }
      ]
    
  • Check for every Result if it is accepted or rejected

    End the Looping here

    • Save in the Database and then return response

If you found this useful or have questions while building with Judge0, drop a comment or reach out—I’d love to hear what you're building!