Happy New Year!
So, I’ve been thinking lately about ways to add coding challenges to an interview process. They, of course, would be set up in an isolated account (AWS)/project(GCP)/subscription (AZURE). They would have environment variables that could change the answer.
For this example, I’ll be using an unauthenticated GCP Cloud Function with an HTTP trigger. If you wanted to recreate this, don’t forget to create a service account when creating the function to avoid using the App Engine default service account. You know, that service account with Editor, aka Demigod access to your project.
import osdef create_haystack(idx: int) -> dict:
haystack = ["hay"] * 99999
haystack.insert(idx, "needle")
challenge = {
"At what index is the needle?": haystack
}
return challengedef stack(request):
INDEX = int(os.environ['CHALLENGE_INDEX'])
return create_haystack(INDEX)
In the above function, when the user makes a GET request, a dictionary is returned with list of 99999 strings of “hay” and 1 string of “needle”. They will need enumerate through the list to find the index set by the environment variable. In the case, it is set to 86315 as shown below.
Solution:
import requestsurl = "https://region-project-id.cloudfunctions.net/function-name"
req = requests.get(url)
data = req.json()
for key, value in data.items():
for index, h in enumerate(value):
if h != "hay":
print(index, h)
Depending how they solve it, this would demonstrate the ability to use requests, parse a dict, and enumerate a list. Maybe this could be a paired with another function to POST the answer to which unlocks the next challenge. Of course, this would come with some limitations if the challenges needed actual runtime (9 minute limit on Cloud Functions for example).
$ 86315 needle
Just thought I’d share an idea, cheers!