Read verdicts, time and memory limits, and hidden tests, and know why a solution fails.
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.
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;
}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";
}This version uses a 32-bit total and passes the three visible examples.
This version prints the total and has no return line.
This version runs for i in range(len(amounts) + 1) and adds amounts[i].
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.
systemdesignmastery.dev/dsa/cheatsheets/how-the-judge-works