Code snippets

This feature is enabled for all our customers. 

A code snippet is a boilerplate code that can be used without changing it. This code helps such candidates who are not familiar with the online coding environment to focus more on the algorithm that is required to solve the question rather than understanding the STD INPUT and OUTPUT syntaxes. 

When a candidate writes code, it is evaluated using input (STDIN) and output (STDOUT). 

  • STDIN: The default source of providing input to any computer program.
  • STDOUT: The default channel where the output of the program is written.


In HackerEarth Assessment's in-built library, code snippets can be generated for Programming questions in the following languages:

  • C
  • C++
  • C++14
  • C++17
  • C#
  • Java
  • Java 8
  • JavaScript(Node.js)
  • PHP
  • Python
  • Python 3
  • Ruby
  • Go
  • Kotlin
  • Objective-C
  • Python 3.8
  • Ruby
  • Scala
  • Swift

These code stubs are already loaded in the code editor of a question for candidates to use when they are attempting the test.

Use of code snippets

Candidates who take online coding tests are usually not familiar with the online coding process. There may not be enough time for a candidate to understand HackerEarth’s evaluation process—specifically how the input (STDIN) and output (STDOUT) must be written to ensure that the evaluation process runs correctly.

If the code snippet is already available, then the candidate can do the following:

  • Focus on writing the logic that is required to solve the problem
  • Not spend time figuring out the input (STDIN) and output (STDOUT) that needs to be used


A candidate must write the code to add N numbers (1, 2, 3…N). The code will be as follows:

Note: The code has been written in different colors only for clarity. In this code, the colors mean the following:

  • Blue = Input
  • Orange = Logic
  • Green = Output


Add the input and output in the form of a code snippet to this question. This becomes visible to the candidates when they are attempting this question. The candidates can then focus only on writing the logic required to calculate the sum of N numbers. 


Generating code snippets

To generate code stubs for a Programming question, follow these steps:

  1. Log in to your HackerEarth Assessment account by using your admin credentials.
  2. Click Tests.
  3. Click the name of any tests in which you want to add code stubs to its Programming questions. For example, Algorithms test.
  4. In the Test details section, click Questions.
  5. Navigate to the Programming questions section and click the name of the question. The modal containing different parts of the question is displayed.
  6. Click Edit question.
  7. In the Edit Programming question section, click Languages.
  8. Navigate to the Code snippets section and click Auto generate.
  9. Enter or select the following details:
    • Name of the function, for example, func
    • Return type, for example, Integer
    • Any comments that you want to add in your code, for example, mycode
    • Notify whether there are multiple test cases in a single file
    • Function parameter and name of the parameter, for example, Character and test
      Important: If you want to add multiple parameters, then click Add parameters.
  10. Click Generate code.
  11. To view your generated code, navigate to the Generated code preview section. 
  12. Click the name of the supported programming languages to view the generated code snippet.
  13. Click Use generated code to use all the generated code snippets in the test. A confirmation message is displayed on the screen asking your permission to replace the existing code if any with the currently-generated code. Click Overwrite to use the generated code. If you wish to use the existing code, then click Don't use generated code.
    Click Cancel if you do not want to save the created code snippets. A confirmation message is displayed on your screen checking whether you want to discard the code snippets that you have created. Click Discard generated code to reject the automatically-generated code. If you want to keep your code, then click Cancel.
  14. Click Publish changes to save your code snippets into the question.

You have successfully generated code snippets for your Programming questions.

You need to manually write or auto-generate code snippets for each language in which you want the candidates to attempt the question.