What a Practice Problem Really Tests
Practice sites such as LeetCode hand you a short story, two or three examples, a list of limits and an empty function. You press Submit, and a judge runs your code against dozens of tests you never see, under a time limit. Passing the examples on the page is where the work starts, not where it ends.
The rule
The limits set your speed; the hidden tests hunt your edges.
Every problem has four parts, and beginners read only the first two:
- The story says what to compute.
- The examples show a few inputs and outputs.
- The limits say how big and how strange the input can get: "1 ≤ n ≤ 100,000", "values may be negative".
- The signature fixes the function name, inputs and return type.
The limits are the most useful line on the page. A judge usually allows about a second, and plain Python manages very roughly ten million simple steps in that time. So the largest n tells you which growth rate you can afford:
| Largest input | Affordable work | What usually fits |
|---|---|---|
| about 20 | 2ⁿ | trying every subset |
| about 1,000 | n² | a loop inside a loop |
| about 100,000 | n log n or n | sorting, one pass, a dictionary |
| a single value up to a billion | log of the value | binary search on the value |
The hidden tests, meanwhile, are written to find the inputs you forgot: nothing at all, one item, ties, negatives, the largest size. A solution that is right "most of the time" is marked wrong.
def busiest_day(steps):
best_day = 0
for day in range(1, len(steps)):
if steps[day] > steps[best_day]: # strictly greater: earliest day wins a tie
best_day = day
return best_dayThe shortcut
Before writing a line, run a four-question scan: How big can n get? Can the input be empty or tiny? Are there ties or duplicates? Are there negatives, zeros or huge values? The first question picks your speed; the other three become your own test cases.
Read the limit
Up to 100,000 days. A loop inside a loop would be about ten billion comparisons, far too slow, so aim for one pass.
The trap is trusting the examples. They are chosen to explain the story, not to break your code. Your scan should produce at least one test the examples did not show.
Drills
Answer fast — trust the rule.
The limits say 1 ≤ n ≤ 100,000. Which approach is too slow?
A problem has at most 15 items and asks about every possible group of them. Can you afford to try every subset?
What does busiest_day([5000, 8200, 8200, 3100]) return if you change > to >=?
The limits say 0 ≤ n ≤ 1000. Which test does that allow that 1 ≤ n would have ruled out?
Remember
- The limits set your speed; the hidden tests hunt your edges.
- Four-question scan: how big, empty or tiny, ties, strange values.
- Trap: the page's examples explain the story; they are not your test suite.
