Unit Testing – How Sites Like LeetCode & HackerRank Test Code for Correctness

microservicestestingunit testingweb servicesweb-applications

Not sure if this question belongs here, but I've always wondered how exactly sites like Leetcode & HackerRank test your code. Say you code in Java for a question like,

Find all the duplicates in an array

After clicking submit, the code is sent as part of a POST request to the backend and then what is the most likely action that happens there?

As a student still, my wild guess is if the code is compiled successfully, it is then test against a test suite specific for Java (I'm picturing a bunch of JUnit tests lol). Perhaps it is a separate microservice which handles the testing and keeping track of the metrics (such as memory usage, how many test cases passed, etc.)?

If so, this would mean each Leetcode question has multiple test suites, with each of them testing for the same test cases but just written in different languages (i.e. JUnit tests for Java, unittest for Python, etc.). I haven't given this a lot of thought, but I'm under the impression this could work. However, would this be the right way of solving such kind of software engineering problem?

Edit: Not asking specifically about Leetcode/HackerRank, but rather from a general software engineering standpoint, if its possible to test in a language agnostic way instead of writing separate test suites for different languages.

Best Answer

If you notice, most of these systems are setup in such a way that you're going to get some well known string input and have to produce some well known string output. At that point, they can take your code and run it as a plain old executable (in some highly disposable sandbox), testing via stdin and stdout while ignoring what language actually created the binary.