</>CodexBash
Learn
First stepsBeginner8 min

How a solution runs here

What the judge does with your code: you write one function, it reads the input, calls you and checks what you return. The same problem in C, C++, Java and Python.

This one leads to

Every problem here asks you for one function. You do not read the input and you do not print the answer. The judge does both, and calls your function in between.

That is worth understanding before anything else, because most first attempts go wrong in the plumbing rather than in the idea.

What actually happens when you press Run#

  1. The judge reads a test case, for example the array [1, 2, 3, 4].
  2. It turns that text into a real value in your language: a list in Python, an int[] in Java, a vector<int> in C++, a pointer and a length in C.
  3. It calls your function with it.
  4. It prints whatever you return, and compares that with the expected output.

So the job is only step 3: take the arguments you are given, work out the answer, and return it.

The same problem in four languages#

Here is "Running sum": given nums, return an array where each position holds the total of everything up to and including it. For [1, 2, 3, 4] the answer is [1, 3, 6, 10].

int* runningSum(int* nums, int numsSize, int* returnSize) {
    int* answer = malloc(numsSize * sizeof(int));
    int total = 0;
    for (int i = 0; i < numsSize; i++) {
        total += nums[i];
        answer[i] = total;
    }
    *returnSize = numsSize;
    return answer;
}

Pick your language on the tabs above. The choice is remembered, so every lesson after this opens on it.

What each language hands you#

The editor starts you with the right signature, so you never have to guess. It still helps to know the pattern:

The problem saysCC++JavaPython
a whole numberintintintint
a large whole numberlong longlong longlongint
an array of numbersint* nums, int numsSizevector<int>int[]list
textchar*stringStringstr
true or falseboolboolbooleanbool

Two things surprise people coming to C:

  • An array arrives as two arguments, a pointer to the first element and how many there are. C arrays do not know their own length.
  • Returning an array needs returnSize. You allocate the result with malloc, set *returnSize to how many elements it holds, and return the pointer. Forget to set returnSize and the judge prints nothing.

Imports are already there#

The judge puts the usual libraries in front of your code, so you do not need to add them:

  • C: stdio.h, stdlib.h, string.h, stdbool.h, limits.h, math.h, ctype.h
  • C++: bits/stdc++.h and using namespace std, which covers vector, string, unordered_map, sort and the rest
  • Java: java.util.* and java.io.*, which covers ArrayList, HashMap, Arrays and friends
  • Python: the standard library is there to import as usual, such as from collections import Counter

Run, then Submit#

Run tries your code on the example cases shown in the problem, and shows you what your function returned next to what was expected. It is quick and nothing is recorded.

Submit runs every test, including hidden ones with the awkward inputs: empty arrays, huge numbers, the smallest and largest sizes allowed. Only a submission that passes every test counts as solved.

Use Run as often as you like. Submit when you think you have handled the edge cases.

Printing while you debug#

Printing to the normal output adds to what the judge compares, so a stray print turns a right answer into a wrong one. Print to the error stream instead. It is shown to you in the test console and never compared:

fprintf(stderr, "total is %d\n", total);

What the verdicts mean#

VerdictWhat happenedWhere to look
AcceptedEvery test passedNowhere. Well done
Wrong answerIt ran, and returned something elseThe failing case: what did you return, and why?
Time limit exceededToo slow for the biggest inputThe approach, usually a loop inside a loop
Runtime errorIt crashedAn index past the end, a null, dividing by zero
Compile errorThe code does not buildThe first error message, and only the first

The first compile error is the real one. Everything after it is usually the compiler confused by the first.

Where to start#

Try Running sum now, in the language you picked. It is small enough that the only thing to learn is how a solution is shaped, which is exactly the point.

Practise it

The same idea, in the order it is worth meeting it.

  1. Not startedRunning sumEasy
  2. Not startedHow many digitsEasy
  3. Not startedTurn the digits aroundEasy
Create a free account to track your progress