Algorithmic problem evaluator using AWS Lambda
There are several platforms that help improve technical skills by exposing algorithmic problems such as leetcode, hackerrank or algoexpert.
These skills are very important, both in daily work and for technical interviews. In this exercise we will do a similar functionality to evaluate algorithmic problems.
These platforms work by exposing you a problem and leaving you to develop the code solution that solves that problem. Evaluating that the answer is correct and that the performance is acceptable.
When submitting your response, they run this code by passing it various test cases and evaluating whether the response is valid.
In this exercise we will try to make a “mock” of how a minisystem would work that does this using AWS Lambda.
Note: All the code written here is not intended for a productive environment.
Problem to solve
The problem that we will take as an example will be this:

Test Cases
For this problem we will have a series of test cases where we know the expected result, for example:
parameters = [38, 102, 1052, 1, 10234]
expected = [2, 3, 8, 1, 1]
What is lambda?
Lambda provides Function as a Service (FaaS), it is a service within AWS that allows you to execute code on demand on self-managed servers.
In this example we will make two versions: The version using Lambda and one using Docker
Creating Lambda Function
1. Go to AWS > Services > Lambda > Functions.
2. Click “Create function”.
We will generate a function that evaluates the test cases passing them through the code written by the user. The user will be asked to write a function called “solution”:
def lambda_handler(event, context):
parameters = [38, 102, 1052, 1, 10234]
expected = [2, 3, 8, 1, 1] result = {} test_passed = 0
for i,p in enumerate(parameters):
ret = solution(p)
if ret != expected[i]:
return {'errorType': 'WrongAnswer', 'errorMessage': 'Error value: '+ str(parameters[i])}
test_passed += 1 result = {'status': 200, 'msg': 'Test passed: '+ str(test_passed)} return result
Solution
The solution in this case is to compute the “digital root”
The digital root of a number is the value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum
def solution(num):
root = 0
while num > 0:
root += num % 10
num = num // 10
if num == 0 and root > 9:
num = root
root = 0
return root
Executing user code
Using lambda there are several ways to execute user code.
To simplify, in this example we will use the simplest one, get it from a preset S3 file:
s3 = boto3.resource('s3')obj = s3.Object("testchallenges", "solution.txt")
content = obj.get()['Body'].read().decode('utf-8')code = compile(content, 'mulstring', 'exec')
eval(code)
Security
It could be considered a security breach to execute code with eval, but various security measures can be taken: from pre-evaluating the code, assigning roles with minimal permissions, assigning few seconds of execution time and little memory for the lambda, etc.
Calling our lambda and getting a result:
client = boto3.client("lambda")response = client.invoke(FunctionName='lambda_challenges', InvocationType='RequestResponse')result = json.loads(response.get('Payload').read())print(result)

It seems that our answer has passed all the tests!
Advantages of Lambda
This version in lambda we can handle various metrics from the configuration:
- How much memory is the maximum used
- What is the timeout in seconds
- A call throttle
Docker version
Docker is a great technology and it provides many advantages.
We can do the same functionality very simply using Docker.
Following the same logic, we can run the code using a container. We can set to run the script on a container, evaluate the response, and shut it down.
Using the same code, we launch an instance, which shares a directory with the local where we have the user’s script, and execute:
docker run -v /var/scripts:/var/scripts — rm -it node:latest python3 /var/scripts/test.py

In this version we have also passed the tests!
Conclusion
Code problem assessments can be useful for technical interviews, education, certifications, and other areas.
Thanks for reading, have a nice day