BeginnerHabits & complexity
The five edge cases to always try
Empty, one element, all the same, negatives, and the biggest allowed input. Most wrong answers hide in these five.
The idea
Code that works on the example often breaks at the edges. Before submitting, run these five through your code by hand: empty input, a single element, all elements equal, negative numbers or zeros, and the largest allowed size.
In interviews, saying them out loud before coding also shows the interviewer you think about correctness.
Worked example
Finding the maximum, the naive way
- Code: best = 0, then best = max(best, x) for each x.
- Empty list → returns 0, but there is no maximum. Decide what to return.
- All negatives [-3, -1] → returns 0, which isn't even in the list. Start best at the first element (or −infinity) instead.
In code (Python)
def maximum(nums):
if not nums:
return None # edge case 1: empty
best = nums[0] # works for all-negative input
for x in nums[1:]:
best = max(best, x)
return bestHow to spot it
- Any problem, every time, before you say “done”.
Common mistakes
- Initialising a running max with 0.
- Indexing nums[0] without checking the list is non-empty.
Practise it
Go further
Up next for you · Beginner
“Have I seen this?” is a set →