Last N items with a slice
last_n() powers the "recent activity" widget. A user set their widget size to 0. What do they see?
def last_n(items: list, n: int) -> list: """Return the last n items (n >= 0).""" if n < 0: raise ValueError("n must be non-negative") return items[-n:]Show answer
line 5 · Wrong edge case
When n is 0, -n is also 0, and items[0:] is the whole list. Asking for zero items returns everything.
return items[max(len(items) - n, 0):]Interview follow-up: Interviewers like this one because the code is correct for every n except the boundary. Ask yourself about 0, 1, and n > len(items).