Creating Units
Creating a Programming Challenge
11min
this tutorial will explain how you can create a coding challenge let's pick a simple problem given a string, you want the user to write a program that return its first character so, for example, if abcd is input to the program, the program must return a the only way to automatically evaluate if a submitted piece of code is correct is to see if it works for a variety of inputs for example, we can input xyz , 123 and op , the program must return x , 1 and o as output these inputs that are used to check the validity of a program are called test cases so, the main components of a coding challenge are a problem statement, preferably written in markdown a list of test cases an editor (ide) where the user writes the program that takes in the input and returns an output the video below shows socratease's interface for exactly this problem boilerplates (driver code) reading user input (standard input) is notoriously hard, especially if one is reading inputs spanning multiple lines so, most programming platforms, including ours, set up some code that does this reading of input this lets the user focus on the core logic, and not worry about input/output for instance, in the video above, you can see that the user doesn't have to do any input/output they just have to fill a function in other products, this is called driver code we call it boilerplate there are two types of boilerplates user boilerplate whatever a candidate sees on the ide this typically has the function name and scope and the user must write the logic within it whatever the ide loads with (as seen in the video above) is the user boilerplate compiler boilerplate code not shown to the user, which takes user input, parses it & then calls the user boilerplate function standard input is always a string the compiler boilerplate's role is to convert the string into a datatype that the user boilerplate can consume for example, if the question is to find the largest integer in a list, the input will be a string like 10 23 2 4 the compiler boilerplate will convert this into a list \[10, 23, 2, 4] different languages will obviously need different code to achieve this so, user and compiler boilerplates are specific to the problem being solved, and the programming language javascript and nodejs both use the nodejs compiler, so use nodejs boilerplate code for javascript the examples below will show you that both javascript and nodejs have identical boilerplates single line input let's continue with our example of extracting the first character of a given string below, you can see the full code that is stored on socratease we are showing the compiler boilerplate as well, though the user will not see it in the ide // user boilerplate function extractfirstchar(word){ 	// write the logic here } // compiler boilerplate let inputbuffer = ''; let inputlines = ''; process stdin setencoding('utf 8'); process stdin on('data', (chunk) => { 	inputbuffer += chunk; }); process stdin on('end', () => { 	inputlines = inputbuffer split('\n'); 	inputbuffer = ''; 	main(); }); function readline() { 	return inputlines shift(); } function main() { 	const word = readline() trim() 	output = extractfirstchar(word); 	console log(output) } typescript # user boilerplate function extractfirstchar(word string) string { \# write the logic here } \# compiler boilerplate process stdin resume(); process stdin setencoding('utf 8'); let inputstring string = ''; let inputlines string\[] = \[]; let currentline number = 0; process stdin on('data', function(inputstdin string) void { inputstring += inputstdin; }); process stdin on('end', function() void { inputlines = inputstring split('\n'); inputstring = ''; main(); }); function readline() string { return inputlines\[currentline++]; } function main() {	 const word = readline(); const output = extractfirstchar(word); console log(output); } \# user boilerplate def extract first char(str) \# write the logic here \# compiler boilerplate inp str = input() print(extract first char(inp str))//user boilerplate import java util scanner; class solution { public character firstchar(string input) { // write the logic here } } //compiler boilerplate class driver { public static void main(string args\[]){ scanner scanner = new scanner(system in); string mystring = scanner next(); scanner close(); solution sol = new solution(); system out print(character tostring(sol firstchar(mystring))); } }//user boilerplate \#include \<iostream> \#include \<string h> using namespace std; char first character(string s) { // write the logic here } //compiler boilerplate int main() { string s; cin >> s; char c = first character(s); cout << c; return 0; }// user boilerplate function extractfirstchar(word){ 	// write the logic here } // compiler boilerplate let inputbuffer = ''; let inputlines = ''; process stdin setencoding('utf 8'); process stdin on('data', (chunk) => { 	inputbuffer += chunk; }); process stdin on('end', () => { 	inputlines = inputbuffer split('\n'); 	inputbuffer = ''; 	main(); }); function readline() { 	return inputlines shift(); } function main() { 	const word = readline() trim() 	output = extractfirstchar(word); 	console log(output) } \<?php // user boilerplate function firstchar($word){ 	 } // compiler boilerplate $word = fgets(stdin); $output = firstchar($word); print($output); c# // user boilerplate using system; public class stringutilities { public char extractfirstchar(string str) { // write your logic here } } // compiler boilerplate class program { static void main(string\[] args) { string inpstr = console readline(); stringutilities su = new stringutilities(); char firstchar = su extractfirstchar(inpstr); console writeline(firstchar); } } kotlin // user boilerplate import java util fun getfirstchar(s string) char { // write your logic here } // compiler boilerplate fun main(args array\<string>) { val sc = scanner(system `in`) val input string = sc next() val output = getfirstchar(input); println(output); }// user boilerplate import 'dart\ io'; string extractfirstchar(string? s) { } // compiler boilerplate void main() { string? inpstr = stdin readlinesync(); print(extractfirstchar(inpstr)); } // user boilerplate def solution(word) end // compiler boilerplate word = gets chomp output = solution(word) puts output you can clearly see that the compiler boilerplate is doing the input and output work for example, in js, the readline() function is reading the string as an input it then calls the extractfirstchar() function and prints to standard output the user needn't concern herself with all this they just need to write the login within the definition of the extractfirstchar() function the image below shows how you would enter the compiler and user boilerplate when you are creating a coding challenge this github url has sample code for reading single lines as strings for other languages to execute an array function, accept a space separated string (for example 4 5 6 7 ) as input you can then split the string and convert it into an array what goes into compiler boilerplate and what goes into user boilerplate isn't clearly defined the more code in compiler boilerplate, the easier it is for the user as they can focus purely on the logic test cases as mentioned earlier, test cases are used to verify if the submitted code is valid or not there are two types of test cases sample test case shown to the user so that they can verify if their code is working or not, before they submit typically, around 3 sample test cases are added to a problem real test case these test cases are hidden from the user (otherwise, the user can write code that returns exactly the output that is expected) there can be 5 10 real test cases and they are checked when the user finally submits the code the points scored by a user are pro rated based on how many real test cases pass you can see real and sample test cases in the image above the total number of test cases (including both sample and real test cases) that can be added is 16 multi line input more complex problems need multiple parameters it is easiest to split the multiple parameters on different lines for example, suppose you the problem is to find an element in an array, given its index the input can then be across three lines 4 5 6 3 2 3 here, 4 in the first line refers to the number of elements in the array 5 6 3 2 in the next line refers to the zero indexed array \[5, 6, 3, 2] and 3 in the last line denotes the index of which we want the value obviously, the answer here is 2 below, you can see how the compiler boilerplate is converting the 3 lines into structured data index and arr // user boilerplate function getvalueatindex(index, arr) { // write the logic here return; } // compiler boilerplate let inputbuffer = ''; let inputlines = ''; process stdin setencoding('utf 8'); process stdin on('data', (chunk) => { 	inputbuffer += chunk; }); process stdin on('end', () => { 	inputlines = inputbuffer split('\n'); 	inputbuffer = ''; 	main(); }); function readline() { 	return inputlines shift(); } function main() { 	const numofelements = parseint(readline() trim()) 	const input arr = readline() split(' ') map((i) => parseint(i)) 	const index = parseint(readline() trim()) 	const s = getvalueatindex(index, arr); 	console log(s) } typescript // user boilerplate type params = { index number; arr number\[]; }; // define a type for the return value type result = number | undefined; // define the function with type annotations function getvalueatindex({ index, arr } params) result { // write the logic here if (index >= 0 && index < arr length) { return arr\[index]; } else { return undefined; } } // compiler boilerplate let inputbuffer = ''; let inputlines string\[] = \[]; process stdin setencoding('utf 8'); process stdin on('data', (chunk string) => { inputbuffer += chunk; }); process stdin on('end', () => { inputlines = inputbuffer split('\n'); inputbuffer = ''; main(); }); function readline() string { return inputlines shift()!; } function main() { const input arr = readline() split(' ') map((i) => parseint(i)); const index = parseint(readline() trim()); const s = getvalueatindex({ index, arr input arr }); console log(s); } \# user boilerplate def get value at index(index, arr) \# write the logic here return \# compiler boilerplate num of elements = int(input()) arr = list(map(int, input() strip() split())) index = int(input()) print(get value at index(index, arr))//user boilerplate import java util scanner; class solution { public getvalueatindex(int index, int arr\[]) { //write your code here } } //compiler boilderplate class driver { public static void main(string args\[]){ scanner sc = new scanner(system in); int numofelements = sc nextint(); int arr\[] = new int\[numofelements]; for (int i = 0; i < numofelements; i++) { arr\[i] = sc nextint(); } int index = sc nextint(); solution sol = new solution(); system out print(sol getvalueatindex(index, arr)); } }//user boilerplate \#include \<iostream> \#include \<string h> using namespace std; char get value at index(int index, int arr\[]) { //write your code here } //compiler boilerplate int main() { int numofelements; cin >> numofelements; int arr\[numofelements]; for (int i=0;i\<numofelements;i++) cin >> arr\[i]; int index; cin >> index; char c = get value at index(index, arr); cout << c; return 0; }// user boilerplate function getvalueatindex(index, arr) { // write the logic here return; } // compiler boilerplate let inputbuffer = ''; let inputlines = ''; process stdin setencoding('utf 8'); process stdin on('data', (chunk) => { 	inputbuffer += chunk; }); process stdin on('end', () => { 	inputlines = inputbuffer split('\n'); 	inputbuffer = ''; 	main(); }); function readline() { 	return inputlines shift(); } function main() { 	const numofelements = parseint(readline() trim()) 	const inputarr = readline() split(' ') map((i) => parseint(i)) 	const index = parseint(readline() trim()) 	const s = getvalueatindex(index, inputarr); 	console log(s) }\<?php // user boilerplate function getvalueatindex($index, $arr){ 	 } // compiler boilerplate $numofelements = intval(fgets(stdin)); $arr = explode(" ", fgets(stdin)); $index = intval(fgets(stdin)); $output = getvalueatindex($index, $arr); print($output); this github url has sample code for reading multiple lines as strings for other languages import via github url on creating a new coding challenge, you'll see an input field as shown below if you have the question in a specific format on a public github repo, you can import it directly from there, instead of filling out all the fields this github url has a sample question you can use a different github url, of course once you paste this url into the input field and click on the fetch button, the question related fields get auto populated you can see this in action in the gif below