visit
We have a single lambda function that takes care of registering a new user
and authenticating an existing user in the application.
API gateway will invoke the lambda function which will get information
about the question/problem statement from the “Questions” table in
Dynamodb (data looks like below).
From the table data, we take the “GeneratedCode” attribute value and
replace various tokens like — name — , — codeworking — & — testcases
— with the class name (generated), the user typed code, and test cases
(from columns “Test_1”… “Test_N”). Here is how the replaced code will
look:
// --name--
public class f_sdsds_a11223_dd {
// --codeworking--
static class Solution{
public static int calculate(String s){
int count=0;
int index=0;
while(true){
try{
s.charAt(index++);
count++;
}catch(Exception e){
break;
}
}
return count;
}
}
public static void main(String args[]) {
int actual=0;
int expected = 3;
String input = null;
// --testcases--
input = "Hi1";
expected = 3;
actual = Solution.calculate(input);
assert actual == expected : String.format("--helper-- Input %s -> Expected = %s but got = %s", input, expected, actual);
}
}
Since Lambda containers don't have JDK installed we have to package
“tools.jar” inside the deployment package so we can run the compilation
inside the containers, here is the code snippet on how we run compile
inside lambda:
try (StringInputStream inputStream = new StringInputStream(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorOutputStream = new ByteArrayOutputStream()) {
Class<?> javacTool = Class.forName("com.sun.tools.javac.api.JavacTool");
Method create = javacTool.getMethod("create");
JavaCompiler compiler = (JavaCompiler) create.invoke(null);
compiler.run(inputStream, outputStream, errorOutputStream, path.toFile().getAbsolutePath());
return new ProcessResponse(replaceNewLine(outputStream), replaceNewLine(errorOutputStream));
} catch (Exception e) {
return new ProcessResponse(null, "Compilation error --> " + e.getMessage());
}
java -ea -cp /tmp/f_sdsds_a11223_dd
We utilize DynamoDB streams to update the total points and total execution time for any given user. A lambda function listens to all updates done on “UserSubmission” table and performs these calculations once the
submission is successful.
To find the winners, we run a simple query to get the top 3 rows in
“UserStastics” table generated by sorting “TotalPoints” column in
descending order and “TotalExecution” column in ascending
I know this doesn't solve all the use cases that a popular code challenge websites like “leetcode” or “topcoder” solves. But this is a good start & foundation which we can keep adding on.If you like this post then leave a comment. That way, I know you have made it all the way to the end 😀 . Thanks again for reading.
Previously published at