3

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.

4
  • sorry if it came across as me asking specifically how Leetcode does it. Rather, I just threw in a common example b/c I see a lot of sites now which allow you to input code and I was just wondering how they are tested Oct 19, 2019 at 6:49
  • 1
    Yes they rely on microcontainers, to compile the code (if needed), and to run the code on the test input. The code you provided is inserted into (merged with) a skeleton source code file - when combined, the result is a self-contained command-line program (it will have a "Main method", accepting command-line arguments, and returning an exit code, for example). During testing, the input data is specified on the command-line and loaded into the program by the skeleton code, passed to your code, your algorithm is run, and the result is saved to a file. The result is then compared with the answer.
    – rwong
    Oct 19, 2019 at 7:03
  • Hmmm that is interesting. Really appreciate the answer! Oct 24, 2019 at 8:41

1 Answer 1

10

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.

1
  • 1
    I've used the HackerRank hiring-side interface, and it let me write a skeleton program the testee never sees, which invokes the function they're asked to write. The whole thing is munged together and compiled, and I also get to provide simple test cases in the form of input and expected output. I can mark some test case(s) as visible to help the testee, but usually many more will be hidden from them.
    – Useless
    Oct 19, 2019 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.