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#
- The judge reads a test case, for example the array
[1, 2, 3, 4]. - It turns that text into a real value in your language: a
listin Python, anint[]in Java, avector<int>in C++, a pointer and a length in C. - It calls your function with it.
- 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;
}
class Solution {
public:
vector<int> runningSum(vector<int> nums) {
vector<int> answer(nums.size());
int total = 0;
for (int i = 0; i < (int) nums.size(); i++) {
total += nums[i];
answer[i] = total;
}
return answer;
}
};
class Solution {
public int[] runningSum(int[] nums) {
int[] answer = new int[nums.length];
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
answer[i] = total;
}
return answer;
}
}
def runningSum(nums):
answer = []
total = 0
for x in nums:
total += x
answer.append(total)
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 says | C | C++ | Java | Python |
|---|---|---|---|---|
| a whole number | int | int | int | int |
| a large whole number | long long | long long | long | int |
| an array of numbers | int* nums, int numsSize | vector<int> | int[] | list |
| text | char* | string | String | str |
| true or false | bool | bool | boolean | bool |
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 withmalloc, set*returnSizeto how many elements it holds, and return the pointer. Forget to setreturnSizeand 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++.handusing namespace std, which coversvector,string,unordered_map,sortand the rest - Java:
java.util.*andjava.io.*, which coversArrayList,HashMap,Arraysand 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);
cerr << "total is " << total << endl;
System.err.println("total is " + total);
import sys
print("total is", total, file=sys.stderr)
What the verdicts mean#
| Verdict | What happened | Where to look |
|---|---|---|
| Accepted | Every test passed | Nowhere. Well done |
| Wrong answer | It ran, and returned something else | The failing case: what did you return, and why? |
| Time limit exceeded | Too slow for the biggest input | The approach, usually a loop inside a loop |
| Runtime error | It crashed | An index past the end, a null, dividing by zero |
| Compile error | The code does not build | The 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.