Sphere Online Judge

How to cope with SPOJ?

This tutorial explains how to use SPOJ. After reading it using SPOJ should be a pure pleasure :)

Registering and changing profile

To register click here. I think destiny of all fields is clear and they don't need to be described, except these two:

  • Send me submission confirmations - check this box if you wish to receive e-mail with information about the status of your submitted solutions (after every submission). All mail is sent to the specified email address. The e-mail will contain the submission number (unique throughout Sphere Online Judge operation), the name of the problem you attempted to solve and the result of the assessment process (including a short error description, if one was detected).

  • Default programming language - allows you to select your first preference when submitting solutions (this is overridden only if you have just submitted a solution in a different language or if the problem you are solving cannot be solved in your preferred language)

Main SPOJ, contests and SPOJ in other languages

This tutorial is based on main SPOJ, but there are also sub-domains dedicated to Vietnamese, Brazilian, Kyrgyz and Polish users. Furthermore, SPOJ hosts many contests organized by various people, schools, universities and organizations; some of contests are public and everyone is invited to participate in them. Using contests is very similar to using main SPOJ.

Choosing a problem to solve

Choosing first problem is one of the greatest... problems for beginners, since SPOJ gives you access to a vast problem archive. For present, the best indicator of problems' difficulty level is number of users who had solved it and percentage of accepted solutions (in the near future we plan to build a tag system that will allow to categorize problems and enable convenient navigation through problemsets). Depending on your skills and experience, you can choose easier or harder problems. Everyone will find something suitable, that's for sure.

Problems are currently divided into four categories:

  • classical - binary scored problems: Accepted or Wrong answer

  • challenge - problems that enable submitting worse or better solutions

  • tutorial - like classical but easier or with generally known algorithm; for educational purpose

  • partial - like challenge but for educational purpose

To view problem list click problems menu item on the left. This is what you should see:


USERS column shows how many users solved the problem. ACC % column shows percentage of solutions to the problem that were accepted. Pointing to a number in USERS column allows to preview number of points you get for solving the problem (more about points and ranking later in this tutorial).

Submitting a solution

Brief description:

After choosing problem from one of the problemsets use the submit link at the top of the page. Alternatively, if you know the code of the problem to which you are submitting the solution, you can use the submission panel. The code of the problem you are solving by default corresponds to the code of the problem you read directly before.

Detailed help:

Let's take a look at the example problem on SPOJ, named Harry and big doughnuts:


Each problem consists of:

Problem description:

Young Harry was asked to buy some foodstuff to his neighbour, weird old lady who owed a lot of fat cats. But cats were weird too and they ate only doughnuts. So the lady wanted (...)

Input description:

There is a single positive integer t (t <= 100) on the first line of input which corresponds to the number of tests (Harry was asked to buy doughnuts few times). Then t lines (...)

Output description:

t lines containing word "yes" if Harry is capable of handling the task or "no" if doughnuts would cause his back crack.

Input example:

5 15 3
1 5 4
13 25 2

Output example:


Additional information:

Added by - problem author
Date - problem creation date
Time limit - how long your solution can perform calculations; if your program doesn't end after specified time limit, you get the Time Limit Exceeded result
Source limit - how big the solutions you upload can be
Languages - which programming languages are allowed for the problem

Input and output data used by SPOJ judge to decide whether your solution is correct or not are set by problem-setter (person who adds problems) and are hidden. Many of problems has tricky test data so the ability to form you own and comprehensive test cases is very useful. For the present there is no possibility to test your solution online with your own test cases (interactively or not) but we plan to introduce this functionality in the near future.

Now let's try to solve DOUGHNUT problem.

Here comes the analysis of the problem and solution to it so if you want to solve the problem on your own, write a solution first and come back here later :)

After reading the description we find out that we need to help Harry... But what really matters to us is meaning of the numbers: c, k and w.

Old lady has c cats so Harry has to buy c doughnuts. Each weights w kilograms. Harry can carry only k kilograms. I guess everyone now knows how the correct formula looks like.

Sample code in C++


  1. #include <cstdio>
  4. int main() {


t for multiple test cases
c, k, w for problem data

Problem's description says that each number is less than or equal to 100 so integer (32bit) is big enough to handle all calculations (even short integer (16bit) would suffice).

  1. int t, c, k, w;

Reading input and code end:

  1. scanf("%d", &t);
  2. while(t--) {
  3. scanf("%d %d %d", &c, &k, &w);
  5. }
  7. return 0;
  8. }

And the main logic:

  1. if(c*w <= k)
  2. printf("yes\n");
  3. else
  4. printf("no\n");

To submit solution click submit link and either paste code into provided text field or upload source code from file. Then pick the language of your submission and click Send button.

You will be redirected to status page where you can watch the progress of judging your solution. After few moments you will see the result:

    0. Waiting - your program has not yet been assessed, please wait a few seconds longer. In some rare case, testing may take up to 2-5 minutes.

    1. AC - accepted - your program ran successfully and gave a correct answer. Congratulations, you have solved the problem!

    2. WA - wrong answer - your program ran successfully, but gave an incorrect answer. Most probably your program contains a bug, or you are not interpreting the problem text correctly.

    3.TLE - time limit exceeded - your program was compiled successfully, but it didn't stop before time limit. This may be because the algorithm is badly designed (too slow), or because it contains a bug, e.g. it goes into an infinite loop, or hangs up, expecting some input data.

    4.CE - compilation error - your program couldn't be compiled; the details of the compiler error can be accessed from the www interface. Note: only some programming languages can give a CE, syntax errors in interpreted languages may instead be displayed as WA (for example, in Python there is no pre-checking of syntax, and in Perl, CE will only occur sometimes if the error is detected during a preliminary syntax check).

    5. RE - runtime error - your program was compiled successfully, but it exited with a runtime error or crashed. You will receive an additional error message, which is most commonly one of the following:

    • SIGSEGV (signal 11) - the most common error for non-interpreted languages: a "segmentation fault" of the program. This may be caused e.g. by an out-of-scope array index causing a buffer overflow, an incorrectly initialized pointer, etc.
    • SIGXFSZ (signal 25) - "output limit exceeded". Your program has printed too much data to output (typically more than 25MB).
    • SIGFPE (signal 8) - "floating point error" - an error of the arithmetic co-processor, such as division by zero, etc.
    • SIGABRT (signal 6) - raised by the program itself, e.g. by making the abort() system call. Note that STL in C++ can raise this signal under some conditions, e.g. due to insufficient memory.
    • NZEC (non-zero exit code) - this message means that the program exited returning a value different from 0 to the shell. For languages such as C, this probably means you forgot to add "return 0" at the end of the program. For interpreted languages (including JAVA) NZEC will usually mean that your program either crashed or raised an uncaught exception.
    • other - there are other signals which can cause program to terminate, all the remaining ones are simply displayed as other.

I hope you got AC at the first time. If not - don't worry; you can resubmit your solutions as many times as you wish.

Now, when you have solved your first problem, you should be ready to face SPOJ on your own. I suggest solving the "Life, the Universe, and Everything" problem next (http://www.spoj.com/problems/TEST/, there are examples how to do it on forum).


If you think that some problem has an incorrect test data, its specification isn't clear or in your opinion something in the problem needs clarification, visit forum or post a comment (this is the main way of communicating with problem-setters) on the bottom of the problem's page. While posting comments, please follow these rules:

1. Don't post any source code.
2. Leave short comments only, don't spam.
3. For more discussion (hints, ideas, solutions) please visit forum.
4. Authors are allowed to delete the comments and use html code (e.g. to provide some useful links).

Source code of your solutions

History of you solutions is available in your profile (my account → history of submissions). To check the source code of your submission click on the ID number in submission list (i.e. status page, history of submissions; you can do the same when you go to problem page and click on My submissions, All submissions, Best solutions). Source code of your submissions is visible only to you.


You can check your position in main ranking by clicking my account link and then Current world rank: XXX, or just by clicking ranks link in the menu on the left side of page.

Formula used for calculating the scores is still evolving. Its current version is described underneath list of users in the ranking (bottom of the page).

How to download all problems within one click?

Go to problem list, check Document type option and click the booklet link.

Memory and time constraints for solutions

The memory limit depends on which cluster was chosen by problem setter (author of a problem) and it may vary among problems. You can read more about SPOJ clusters here. The time limit for each test data file of each problem may be different. If the time limit is 3 seconds (shown below description of the problem) you may assume that the time limit for each test data file is 3 seconds. If the time limit is 1-3 seconds, you may assume that there are multiple test data files, the shortest time limit for solving some data set is 1 second, and the longest is 3 seconds.

What is segmentation fault? (runtime err. SIGSEGV)

From glibc documentation:

This signal is generated when a program tries to read or write outside the memory that is allocated for it, or to write memory that can only be read. (Actually, the signals only occur when the program goes far enough outside to be detected by the system's memory protection mechanism.) The name is an abbreviation for "segmentation violation".

Common ways of getting a `SIGSEGV' condition include dereferencing a null or uninitialized pointer, or when you use a pointer to step through an array, but fail to check for the end of the array. It varies among systems whether dereferencing a null pointer generates `SIGSEGV' or `SIGBUS'.

How did SPOJ come into being?

If you are interested, here is a brief description: http://www.spoj.com/forum/viewtopic.php?f=6&t=133


For more information, help, hints, ideas and suggestions please visit our forum. FAQ is also situated in the forum.

Good luck!

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.