Online Compiler — Demonstration of SubProcess

Tanishq Rawat
4 min readDec 26, 2022

--

I learned the subprocess module in python. But, during the learning cycle, I was implementing the logic with no real-life implementation.

As a software developer, to achieve this position every individual has to go through various Data structure and Algorithm problems. We grill Leetcode, Codeforces, Codechef, HackerEarth, and N number of platforms. But I wondered how these platforms can compile 20–30 programming language codes and provide the results respectively.

Just take an example that an XYZ platform is hosted on an Apache Tomcat webserver. How during runtime it can compile my C lang code provide its input and in the response return the respective output or if it fails to compile how errors are visible to us in a very simple manner?

In a very simple manner, I demonstrated this by developing a platform that can execute C, C++, Python, and Java code with custom inputs.

Web Application demonstrating the Basic UI of Let-Compile (Online Compiler)

Let us test this application with a code to print all even values existing in a vector. Source code written in C++

//Example1.cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v={1,2,3,4,5,6,7,8,9,0};
for(int e:v)
{
if(e%2==0) cout<<e<<" ";
}
cout<<endl;
return 0;
}
After Uploading the code, In the result section, we can see that execution starts
Code is compiled successfully, and thus we can clearly see the desired output.

Now, knowingly I will put an error in the code by missing a semi-colon in for loop.

//Example2.cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v={1,2,3,4,5,6,7,8,9,0};
for(int e:v)
{
if(e%2==0) cout<<e<<" "
}
cout<<endl;
return 0;
}
Uploaded example2.cpp and now it is been processed.
We can see that due to an error in the code, we can see the error as the code did not compile successfully.

Now, we can try a python code.

#Code to determine maximum subarray sum
def maxSubArraySum(inp):
mx=-2147483648
sm=0
for i in inp:
sm+=i
mx=max(sm,mx)
if sm<0: sm=0
return mx

print(maxSubArraySum([1,-3,4,-1,-2,6]))
#Output should be 7 -- > [4,-1,-2,6] sum=7
The code gives output as 7 which is correct.

Now we can try with a custom test case.

#Code to determine maximum subarray sum
def maxSubArraySum(inp):
mx=-2147483648
sm=0
for i in inp:
sm+=i
mx=max(sm,mx)
if sm<0: sm=0
return mx

n=int(input())
inp=[]
for i in range(n):
inp.append(int(input())
print(maxSubArraySum(inp))
//Text file containing testcase
9
5 -4 -1 11 4 -5 9 -14 -2
//Output should be 19
// [5,-4,-1,11,4,-5,9] sum=19
Result with custom test case file.

How all this was possible?

The idea behind this was simple — “I will determine the source code file, which type of file it is, is it a .py file or .cpp file or .c file or .java file or .js file or any other language. Behind I will run a subprocess which will be the compiler to whom I will pass the source file as input then it will compile and if the respective language is a complied language then an executable will be generated or else it would be interpreted language then respective decision can be made. If there is an error I will send an error in response else I will be sending the output generated by the code.”

The thing which I made is very simple but it can be powerful when I will introduce features like,

  1. Time constraints to tackle time limits exceed issues.
  2. Attractive UI
  3. User Authentication
  4. Simpler error details
  5. Personalized problem-solving question
  6. Thousands of validation for smooth work flow

Many features can be added, not actually this is how those coding platforms are created but yes some of the ground basics are similar.

--

--

Tanishq Rawat
Tanishq Rawat

Written by Tanishq Rawat

SDE @ Mobileum | Ex-SDE Intern at Cerebry

No responses yet