How do sites like LeetCode & HackerRank test your code for correctness?

by OceanMountains   Last Updated October 19, 2019 08:05 AM - source

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.

Answers 1

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.

October 19, 2019 07:05 AM

Related Questions

What is the best method for testing a web app?

Updated November 23, 2018 13:05 PM

Should I unit test

Updated May 16, 2017 11:05 AM


Updated February 18, 2017 10:05 AM

What does stubbing mean in programming?

Updated March 03, 2017 13:05 PM