Python Easy
How do you reverse a list and remove duplicates in Python, and what are the performance implications of each approach?
The short answer
Reversing a list is O(n) whether you use slice notation or list.reverse(). Deduplication is O(n) with a set conversion but O(n²) if you check membership against a list. Understanding when order must be preserved changes which tool to reach for.
How to think about it
This feels like a simple question but it has a performance angle the interviewer usually wants to hear: the difference between O(n) set-based dedup and the O(n²) loop that most beginners write. And the order-preservation question (set vs dict.fromkeys) distinguishes candidates who know the stdlib idioms from those who guess.
Try all the approaches live
When to use each approach
| Goal | Tool | Time | Space |
|---|---|---|---|
| Reverse in place | list.reverse() | O(n) | O(1) |
| Reverse to new list | lst[::-1] | O(n) | O(n) |
| Dedupe, order irrelevant | list(set(lst)) | O(n) | O(n) |
| Dedupe, order preserved | list(dict.fromkeys(lst)) | O(n) | O(n) |
| Dedupe + reverse | list(dict.fromkeys(lst))[::-1] | O(n) | O(n) |