Read verdicts, time and memory limits, and hidden tests, and know why a solution fails.
Use it when a submission fails and you must find out why. Signal words: Wrong Answer on a hidden test, Time Limit Exceeded, Runtime Error, it works on my examples.
Start read the verdict and name the first kind of failure it reports.
Step make a test that causes it: an empty list, the largest values, the largest size. Run it by hand on a small case, then think about the largest case.
Stop the verdict is Accepted, or you can explain the failure with a test you ran.
It does not show you a failing hidden test. You must build one from the verdict, the constraints and your own reading of the code.
Cost: each submission costs one run of the whole test set. A hand trace of a small case costs minutes and finds most bugs first.
In this lesson you learn what happens when you submit a solution. You learn what the judge runs, what its four main verdicts mean, and how to find the cause of each one. Every verdict below comes from running real code.
You work on a payments app. The end-of-day job adds up every payment of the day, and your team lead asks you to write the function that returns the total. Payments are whole numbers in minor units.
You test with 3 payments: 120, 80 and 300. The total is 500, and your function returns 500. Then you submit it to the course judge.
The judge runs your function on many more tests than the three you saw. It gives one verdict for the whole run, and it never shows the hidden tests. So you must know what each verdict means.
Your team lead says: Before you hand it over, tell me which verdicts the judge can give, and what each one tells you.
A practice paper comes with its answers, and you check your work against them. The final exam has more questions, some harder, and it has a time limit. The examiner marks only the final paper.
The practice paper is the visible tests. The final exam is the hidden tests, and the time limit is the judge's time limit. Passing the practice paper does not mean you will pass the exam.
Like a practice paper and a final exam: the examples show the task, and the hidden tests mark you.
Before you look at real verdicts, judge one run by hand. A toy judge gets the expected list, the list your code returned, the time it took and the limit. The player lists every step.
Solve it by hand: The toy judge: one wrong rank. The hand-solve sheet loads in your browser.
The toy judge checks the time first, then the length, then each position.
Four versions of the total function ran on the same four tests. Each cell shows what the run did. Every cell comes from running the code.
| Test | Input | A | B | C | D |
|---|---|---|---|---|---|
| 1 | 3 payments: 120, 80, 300 | 500 | 500 | 500 | IndexError |
| 2 | no payments | 0 | 0 | 0 | IndexError |
| 3 | 3 payments of 1,000,000,000 | 3,000,000,000 | -1,294,967,296 | 3,000,000,000 | IndexError |
| 4 | 100,000 payments, from 1,000,000,000 down by 1 | 99,995,000,050,000 | -428,535,472 | stopped at 2,000 ms | IndexError |
Version A keeps a running total. Version B is the same loop with a 32-bit total, run in Java. Version C rebuilds the total from the first payment for every payment. Version D loops one place past the end.
The verdicts are: A is Accepted. B is Wrong Answer on test 3. C is Time Limit Exceeded on test 4. D is Runtime Error on test 1.
Start with a correct version. The report shows the total so far after every payment, so version C rebuilds the total from the first payment each time and keeps the last one.
def total_of_payments(amounts):
total = 0
for i in range(len(amounts)):
total = 0
for j in range(i + 1): # rebuild the total so far
total += amounts[j]
return totalThis is correct for every input. For 100,000 payments it makes n(n+1)/2 additions.
Now check the real size.
A busy day has 100,000 payments, the judge allows 2 seconds, and a machine does about 100,000,000 simple steps a second.
Here is the count:
So version C needs about 50 seconds, and the limit is 2 seconds. On the test machine it was stopped at 2,000 ms.
Predict the verdict
Q Version C is correct on every test it finishes. It runs on 100,000 payments with a limit of 2 seconds. What verdict does the judge give?
Answer: C
The run takes about 50 seconds and is stopped at the limit. A correct answer that arrives too late is still Time Limit Exceeded.
The next step is to see why version A finishes at once.
Can you make version C finish inside the limit? Look at two neighbouring totals. The total after payment 4 is the total after payment 3, plus payment 4. Rebuilding it from the first payment repeats work that is already done.
Keep a running total, and add each payment once. The loop does n additions, not n(n+1)/2.
The counts below come from running both loops with a counter:
| Payments n | Rebuild from the first | Running total |
|---|---|---|
| 100 | 5,050 | 100 |
| 1,000 | 500,500 | 1,000 |
| 3,000 | 4,501,500 | 3,000 |
| 5,000 | 12,502,500 | 5,000 |
Race: Additions of two ways to total the payments. The operation counter loads in your browser.
The race counts the additions of both loops by running them. At 5,000 payments the rebuild makes 12,502,500 additions and the running total makes 5,000.
Keep a running total: each payment is added once, so five thousand payments need five thousand additions, not twelve and a half million.
Exercise · Predict the output: How many additions does the rebuild make?. Version C rebuilds the total from the first payment after every payment. The code counts its additions on six payments. What does it print?
amounts = [120, 80, 300, 50, 10, 40]
additions = 0
for i in range(len(amounts)):
total = 0
for j in range(i + 1):
total += amounts[j]
additions += 1
print(additions)Now the wide total. Three payments of 1,000,000,000 are added one by one. The player stops at four steps and asks you to predict the total.
Dry run: Predict the total: three payments of one billion. The step-by-step player loads in your browser.
Reading without the player? Cover the last column and predict it row by row.
| Amount number | Amount | Total after it |
|---|---|---|
| 1 | 1,000,000,000 | 1,000,000,000 |
| 2 | 1,000,000,000 | 2,000,000,000 |
| 3 | 1,000,000,000 | 3,000,000,000 |
The third total passes 2,147,483,647, the largest 32-bit integer. A 32-bit total would show -1,294,967,296 there.
The judge runs your function on the tests in order, and the verdict is the result of the first test that fails. So the verdict names the first kind of failure, and it says where to look.
| Verdict | What happened | Where to look first |
|---|---|---|
| Accepted | Every test passed inside the limits | Nothing: read the editorial |
| Wrong Answer | A returned value differed from the expected one | Edge cases, output order, a wrapped total |
| Time Limit Exceeded | The run took longer than the limit | The step count at the largest input |
| Runtime Error | The code crashed | The empty list, an index past the end, a divide by zero |
| Memory Limit Exceeded | The run used more memory than the limit | A copy of a huge list, a table that is too big |
| Compile Error | The code did not compile | The message with the line number |
Exercise · Put it in order: How many tests pass before the first failure?. Arrange the lines into a function that returns how many tests pass before the first wrong answer. The two lists have the same length.
def tests_passed(expected, got):
passed = 0
for i in range(len(expected)):
if got[i] != expected[i]:
break
passed += 1
return passed
continueHere is the rule that gives every verdict in this lesson. Both templates use it.
A run is judged in a fixed order: time, then the shape of the output, then every value. A solution is Accepted only when every test passes.
Use it when a submission fails and you must find out why. Signal words: Wrong Answer on a hidden test, Time Limit Exceeded, Runtime Error, it works on my examples.
Start: read the verdict and name the first kind of failure it reports.
Step: make a test that causes it: an empty list, the largest values, the largest size. Run it by hand on a small case, then think about the largest case.
Stop: the verdict is Accepted, or you can explain the failure with a test you ran.
It does not show you a failing hidden test. You must build one from the verdict, the constraints and your own reading of the code.
Cost: each submission costs one run of the whole test set. A hand trace of a small case costs minutes and finds most bugs first.
Every problem on this course asks for one function. You write only the function: the judge builds the input, calls your function and compares the value it returns with the expected value. Text that you print is not judged. Python and JavaScript run in your browser, and Java and C++ run on the course's server judge.
The three kinds of tests are:
Each problem lists its limits. The usual limits are 2,000 ms for each test and 256 MB of memory.
A checker compares your result with the expected one. Most problems use an exact checker, so the value and its order must match. A few use a tolerance for decimals or accept any order of a list.
The total of 100,000 payments of up to 1,000,000,000 reaches 10^14, so the template uses a 64-bit integer.
def wide_total(amounts):
# [1] Start the total at zero in a type wide enough for totals past 2,147,483,647.
# A Python int has no fixed size, so it cannot wrap.
total = 0
# [2] Take each amount in turn.
for amount in amounts:
# [3] Add it once to the running total.
total += amount
# [4] Return the total.
return totalpublic long wideTotal(int[] amounts) {
// [1] Start the total at zero in a type wide enough for totals past 2,147,483,647.
// A Java long is 64 bits; an int would wrap.
long total = 0;
// [2] Take each amount in turn.
for (int amount : amounts) {
// [3] Add it once to the running total.
total += amount;
}
// [4] Return the total.
return total;
}long long wideTotal(const std::vector<int>& amounts) {
// [1] Start the total at zero in a type wide enough for totals past 2,147,483,647.
// A long long is at least 64 bits; an int is usually 32.
long long total = 0;
// [2] Take each amount in turn.
for (int amount : amounts) {
// [3] Add it once to the running total.
total += amount;
}
// [4] Return the total.
return total;
}function wideTotal(amounts) {
// [1] Start the total at zero in a type wide enough for totals past 2,147,483,647.
// A JavaScript number holds whole numbers exactly up to 9,007,199,254,740,991.
let total = 0;
// [2] Take each amount in turn.
for (const amount of amounts) {
// [3] Add it once to the running total.
total += amount;
}
// [4] Return the total.
return total;
}Rebuild the template: put its lines back in order. Loads in your browser.
The second template is the verdict of one run, in the order the real judge uses.
def judge_verdict(expected, got, elapsed_ms, limit_ms):
# [1] Time first: a run over the limit is Time Limit Exceeded, whatever it printed.
if elapsed_ms > limit_ms:
return "Time Limit Exceeded"
# [2] A list of a different length cannot match.
if len(expected) != len(got):
return "Wrong Answer"
# [3] Compare position by position, in order.
for i in range(len(expected)):
if expected[i] != got[i]:
return "Wrong Answer"
# [4] Everything matched within the limit.
return "Accepted"public String judgeVerdict(int[] expected, int[] got, int elapsedMs, int limitMs) {
// [1] Time first: a run over the limit is Time Limit Exceeded, whatever it printed.
if (elapsedMs > limitMs) {
return "Time Limit Exceeded";
}
// [2] A list of a different length cannot match.
if (expected.length != got.length) {
return "Wrong Answer";
}
// [3] Compare position by position, in order.
for (int i = 0; i < expected.length; i++) {
if (expected[i] != got[i]) {
return "Wrong Answer";
}
}
// [4] Everything matched within the limit.
return "Accepted";
}std::string judgeVerdict(const std::vector<int>& expected, const std::vector<int>& got, int elapsedMs, int limitMs) {
// [1] Time first: a run over the limit is Time Limit Exceeded, whatever it printed.
if (elapsedMs > limitMs) {
return "Time Limit Exceeded";
}
// [2] A list of a different length cannot match.
if (expected.size() != got.size()) {
return "Wrong Answer";
}
// [3] Compare position by position, in order.
for (size_t i = 0; i < expected.size(); i++) {
if (expected[i] != got[i]) {
return "Wrong Answer";
}
}
// [4] Everything matched within the limit.
return "Accepted";
}function judgeVerdict(expected, got, elapsedMs, limitMs) {
// [1] Time first: a run over the limit is Time Limit Exceeded, whatever it printed.
if (elapsedMs > limitMs) {
return "Time Limit Exceeded";
}
// [2] A list of a different length cannot match.
if (expected.length !== got.length) {
return "Wrong Answer";
}
// [3] Compare position by position, in order.
for (let i = 0; i < expected.length; i++) {
if (expected[i] !== got[i]) {
return "Wrong Answer";
}
}
// [4] Everything matched within the limit.
return "Accepted";
}Rebuild the template: put its lines back in order. Loads in your browser.
Exercise · Find the bug: The run that finishes on time. A run takes exactly 2,000 ms with a limit of 2,000 ms and the right answer, yet this toy judge rejects it. Which line is wrong?
def judge_verdict(expected, got, elapsed_ms, limit_ms):
if elapsed_ms >= limit_ms:
return "Time Limit Exceeded"
if len(expected) != len(got):
return "Wrong Answer"
for i in range(len(expected)):
if expected[i] != got[i]:
return "Wrong Answer"
return "Accepted"The judge decides in a fixed order: time first, then the length of the output, then each value, and Accepted only when all pass.
You can find the cause of any verdict in four steps.
A run returns [500], which is the expected answer. It took 2,300 ms and the limit is 2,000 ms.
Template 2 checks the time first, so it returns Time Limit Exceeded without looking at the output. With a time of 2,000 ms it returns Accepted, because a run exactly at the limit is inside it.
Real data brings edge cases.
With amounts = [] the total is 0, and the loop does not run. Version D, which loops one place past the end, reads the first place of an empty list and gives IndexError. Test the empty list first.
A run with elapsed_ms = 2000 and limit_ms = 2000 is Accepted by the toy judge: the check is "over the limit", not "at the limit". The same code takes a slightly different time on each run, so do not plan to finish at the last millisecond.
With amounts = [1000000000, 1000000000, 1000000000] the total is 3,000,000,000. A single payment fits a 32-bit integer, and the total does not. The error shows only when the sum of several large values passes 2,147,483,647.
An exact checker compares lists position by position. For scores = [50, 80, 80, 20] the answer is [3, 1, 1, 4] and [1, 1, 3, 4] is Wrong Answer, although both hold the same values. Return the list in the order the statement asks for.
Exercise · Break it: Find the answer this checker accepts. This checker should accept an answer only when it matches the expected list position by position. Find two lists where it says yes but should say no.
def accepts(expected, got):
return sorted(expected) == sorted(got)This version uses a 32-bit total and passes the three visible examples.
Counterexample: three payments of 1,000,000,000 return -1,294,967,296 in Java, and the expected answer is 3,000,000,000. The judge shows Wrong Answer on that hidden test.
Visible tests show that you read the task. They do not show that the code is correct for every input.
It passes the examples but fails a hidden test: a 32-bit total returns minus 1,294,967,296 for three payments of one billion.
This version prints the total and has no return line.
Counterexample: for 120, 80 and 300 it prints 500, and the function returns None. The judge reads the returned value, which is null, and shows Wrong Answer.
Return the answer. Do not print it.
This version runs for i in range(len(amounts) + 1) and adds amounts[i].
Counterexample: it fails on every list, including the three visible payments: at index 3 it raises IndexError. The judge shows Runtime Error.
The last valid index is len(amounts) - 1.
Counting steps assumes each step costs about the same. It does not in every language. Python took 0.5 to 0.7 seconds for 10,000,000 additions over several runs, so 100,000,000 would take 5 to 7 seconds. C++ finished 100,000,000 additions in about 0.1 seconds.
The times depend on the machine. Use the step count to compare methods, and leave a margin when the language is slow.
Your team lead brings three product changes. Each one changes how the judge compares the result.
A report returns an average as a decimal number. Adding 0.1 ten times gives 0.9999999999999999, not 1.0, so an exact checker would reject it. A tolerance checker accepts a result within, for example, 0.000001 of the expected value.
A report returns the pairs of payments that sum to a target. The order of the pairs does not matter. An unordered checker sorts both lists first, so [[3, 4], [1, 2]] and [[1, 2], [3, 4]] both match.
A job copies a list of 10,000,000 integers. In Python the list needs about 80 MB for its places alone. Building it from range and copying it once peaked at about 495 MB on the test machine. A limit of 256 MB gives Memory Limit Exceeded.
Explain it back: write the idea in your own words and get feedback.
In two or three sentences, explain to a teammate what the four main verdicts mean and how you find the cause of each. The AI tutor checks your answer against the invariant and the key points of this lesson.
Here is how you could explain it in an interview, now that you have seen it run.
The judge calls my function on visible, hidden and large tests, and it reports the first kind of failure. Wrong Answer means a value differed, Time Limit Exceeded means the run was too slow, and Runtime Error means the code crashed.
For a wrong total I check a 32-bit overflow. Three payments of 1,000,000,000 give 3,000,000,000, and a 32-bit total wraps to -1,294,967,296.
For a slow run I count the steps at the largest input. Rebuilding a total each time makes 5,000,050,000 additions for 100,000 payments.
When a submission fails, I read the verdict first, because it names the first kind of failure. Wrong Answer means a returned value differed from the expected one. Time Limit Exceeded means the run was too slow, even if the answer was right. Runtime Error means the code crashed.
For Wrong Answer I try the edge cases: the empty list, one item, the largest values. I check whether a total can pass 2 billion, because a 32-bit total would wrap, and I use a 64-bit integer. I also check that I return the answer and do not print it.
For Time Limit Exceeded I count the steps at the largest input. Rebuilding a total each time makes n times n plus 1, over 2 additions, which is 5 billion for 100,000 items. A running total makes 100,000.
Then I trace a small case by hand with a table.
Checkpoint · How the judge works
Q1/10 On this course you submit one function. How does the judge read your answer?
Answer: B
The judge calls your function and compares the returned value with the expected one. A function that only prints returns nothing.
Q2/10 Which tests does the judge keep out of sight?
Answer: B
The examples are the visible tests. The hidden tests hold edge cases and seeded random cases, and Submit runs them after the visible ones.
Q3/10 Your code passes the three examples and the judge says Wrong Answer. What is the most useful next step?
Answer: B
Wrong Answer on a hidden test points to a case the examples missed. Edge cases and large values are the usual causes.
Q4/10 Three payments of 1,000,000,000 are added in a 32-bit integer. What happens?
Answer: C
A 32-bit integer cannot hold 3,000,000,000, so the value wraps. The fix is a 64-bit integer.
Q5/10 A solution returns the correct answer on every test but takes 50 seconds on the largest one. The limit is 2 seconds. What verdict does it get?
Answer: C
A correct answer that arrives after the limit is still Time Limit Exceeded.
Q6/10 A version rebuilds a total from the first payment for each of 100,000 payments. How many additions does it make?
Answer: C
It makes n(n+1)/2 additions: 100,000 × 100,001 / 2 = 5,000,050,000.
Q7/10 A loop reads amounts[i] for i from 0 up to and including len(amounts). Which verdict does the judge give?
Answer: C
The loop reads one place past the end, which raises IndexError on every list. That is a crash, so the verdict is Runtime Error.
Q8/10 A run returns the correct answer in 2,300 ms. The limit is 2,000 ms. The toy judge checks one thing first. What is it?
Answer: C
The toy judge checks the time before it looks at the output. The run is over the limit, so the verdict is Time Limit Exceeded.
Q9/10 The expected ranks are [3, 1, 1, 4] and your function returns [1, 1, 3, 4]. An exact checker compares them. What is the verdict?
Answer: B
An exact checker compares lists position by position, so the same values in a different order do not match.
Q10/10 Why can a solution pass all the visible tests and still fail?
Answer: C
Visible tests show that you read the task. Hidden tests cover the empty list, the largest values and bigger sizes.
Ten questions on this lesson. Pass with 80% or more to be sure the idea has landed before the next lesson.
| Step | Problem | Product line | Level | What it practises |
|---|---|---|---|---|
| 1 | Pages needed for a result list | Search | Easy | Rounding up with integers and a 64-bit total |
| 2 | Total of very large ids | Freight | Easy | A set for seen values and a total past 32 bits |
| 3 | Leaderboard with tied scores | Feed | Medium | Ties, the order of the output and a time limit |
Review cards from this lesson.
Five cards from this lesson join your spaced-review deck. The first one comes back tomorrow.
Step through the idea on your own input, then build it in the lab. Runs in your browser. Step through it.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.