Skip to content

The detective's checklist

Read · 1 of 2

Five questions

1. What exactly should it do? Pin down the rules first: what comes in, what should come out.

2. Does it work on a normal input? Try one small example by hand.

3. What about the edges? Empty, one item, all the same, negatives, zero, the biggest allowed values.

4. Is it fast enough for the biggest input the problem allows?

5. What if the input is unexpected? Does it crash, or say so clearly?

Read this lesson as text

Five questions to ask about any piece of code.

Five questions

1. What exactly should it do? Pin down the rules first: what comes in, what should come out.

2. Does it work on a normal input? Try one small example by hand.

3. What about the edges? Empty, one item, all the same, negatives, zero, the biggest allowed values.

4. Is it fast enough for the biggest input the problem allows?

5. What if the input is unexpected? Does it crash, or say so clearly?

Second biggest

n = int(input())
nums = list(map(int, input().split()))
print(sorted(nums)[-2])

This should print the second biggest different value, or -1 if there isn't one. Which input exposes the bug?

  • 3 then 1 2 3
  • 3 then 5 5 5
  • 2 then 7 1
Show the answer

3 then 5 5 5. With 5 5 5 there's no second different value, so the answer is -1, but sorted(nums)[-2] still gives 5. "All the same" is on the checklist for exactly this reason.