Welcome to HackerRank DSL (Domain Specific Language) Documentation! You can use our DSL to generate code stubs that read test case data from standard input in HackerRank challenges.
2. Supported Data Types
The following data types are supported for reading variables, arrays, linked lists, and binary search trees:
- Boolean:
boolean
- Character:
character
- Integer:
integer
- Long Integer:
long_integer
- Float:
float
- Double:
double
- String:
string
3. Reading and Printing Variables
To read a variable the DSL is DATA_TYPE(VARIABLE_NAME)
. For example, to read an integer, n, the DSL is integer(n)
. Reading space separated variable is also supported. For example, to read two integers, n and m separated by space, the DSL is integer(n) integer(m)
.
To print a variable the DSL is print(DATA_TYPE, VARIABLE_NAME)
. Note that only one print each statement per line is supported.
Example
DSL Example: |
Sample Input |
Generated Code: |
4. Reading and Printing 1D Arrays
To read the array given that elements are given separated by space, the DSL is array(DATA_TYPE, VARIABLE_NAME, ELEMENT_COUNT, single)
and to read the array such that elements are given each on a new line, the DSL is array(DATA_TYPE, VARIABLE_NAME, ELEMENT_COUNT, multi)
. Here, ELEMENT_COUNT could be:
- An integer constant describing the total number of elements.
- An integer variable which is already read.
- A simple expression consisting of integer constants, integer variables, and, (+, −, ×) operators only.
- Underscore which suggests reading the total number of elements before reading the array.
To print an array the DSL is print(DATA_TYPE_array, VARIABLE_NAME, SEPARATOR)
. The separator is optional and default value is SPACE. The valid values are COMMA, SPACE, NEWLINE, and _ (Underscore).
Example
DSL Example: |
Sample Input |
Generated Code: |
5. Reading and Printing 2D Arrays
To read 2D array the DSL is 2darray(DATA_TYPE, VARIABLE_NAME, ROW_COUNT, COLUMN_COUNT)
. Here ROW_COUNT and COLUMN_COUNT could be:
- An integer constant describing the total number of rows and columns.
- An integer variable which is already read.
- A simple expression consisting of integer constants, integer variables, and, (+, −, ×) operators only.
- Underscore which suggests reading the total number of rows and columns before reading the array.
To print the 2D array the DSL is print(2d_DATA_TYPE_array, VARIABLE_NAME)
. The array is always printed in matrix form, i.e., all the elements of each row are printed space separated and each row is printed on a new line.
Example
DSL Example: |
Sample Input |
Generated Code: |
6. Reading and Printing Linked Lists
The DSL supports singly and doubly linked lists. The DSL to read singly linked list is DATA_TYPE_singly_linked_list(VARIABLE_NAME)
and to read doubly linked list is DATA_TYPE_doubly_linked_list(VARIABLE_NAME)
. The generated code first reads the total number of nodes followed by the node values each on a new line. The DSL supports reading only one linked list.
The DSL to print singly linked list is print(DATA_TYPE_singly_linked_list, VARIABLE_NAME, SEPARATOR)
and to print doubly linked list is print(DATA_TYPE_doubly_linked_list, VARIABLE_NAME, SEPARATOR)
. The separator is optional and default value is SPACE. The valid values are COMMA, SPACE, NEWLINE, and _ (Underscore).
Example
DSL Example: |
Sample Input |
Generated Code: |
7. Reading and Printing Binary Search Trees
To read binary search tree the DSL is DATA_TYPE_binary_search_tree(VARIABLE_NAME)
. The generated code first reads the total number of nodes followed by the node values each on a new line in order of insertion. The DSL supports reading only one binary search tree.
The DSL to print binary search tree is print(DATA_TYPE_binary_seach_tree, VARIABLE_NAME, SEPARATOR)
. The separator is optional and default value is SPACE. The valid values are COMMA, SPACE, NEWLINE, and _ (Underscore).
Example
DSL Example: |
Sample Input |
Generated Code: |
8. Reading and Printing Graphs
The DSL supports reading integer weighted graph and integer unweighted graphs using the DSL weighted_integer_graph(VARIABLE_NAME)
and unweighted_integer_graph(VARIABLE_NAME)
respectively. The generated code first reads the total number of nodes and edges separated by space followed by each of edges in the form start-node end-node and optional weight separated by space. Note that all the inputs must be integer only.
The DSL does not support printing graphs.
Example
DSL Example: |
Sample Input |
Generated Code: |
9. Declaring and Invoking Functions
The DSL to define function is function(RETURN_TYPE, FUNCTION_NAME, PARAM1—TYPE PARAM1—NAME, PARAM2—TYPE PARAM2—NAME, …, PARAMn—TYPE PARAMn—NAME, #FUNCTION_BODY_COMMENT)
. If the function does not return anything, the return type should be void. The function parameters and body comment are optional.
To invoke a function, the DSL is invoke(RETURN_TYPE, RETURN_VARIABLE_NAME, FUNCTION_NAME, PARAM1—NAME, PARAM2—NAME, …, PARAMn—NAME)
. If the function return type is void then the return variable name could be replaced with _ (underscore).
Example
DSL Example: |
Sample Input |
Generated Code: |
10. Writing Loops
The DSL supports performing repeated tasks inside a loop. The DSL syntax for loops is:
loop(VARIABLE_NAME) VALID_DSL_STATEMENT endloop
Here, VARIABLE_NAME is an integer variable which is already read and describes the total number of times the loop must run. The DSL also supports nested loops.
Example
DSL Example: |
Sample Input |
Generated Code: |
1. Writing Comments
The DSL supports custom comments in the form
#YOUR_COMMENT
. A special comment#StartCode
generates "Write your code here".Example
DSL Example:
Generated Code: