Loading section...
sorted() vs .sort() — What Interviewers Are Actually Watching
Here's something most tutorials won't tell you: interviewers at top companies don't care if you know that sorted() returns a new list and .sort() is in-place. Every candidate knows that. What they're watching is whether you reach for the right one given the context — and whether you can explain why. The Core Syntax The key= Parameter — The Heart of the Pattern The key= parameter is where custom sorting lives. It's a function that takes one element and returns a value Python uses for comparison. The critical insight: Python calls your key function exactly once per element, caches the results, and uses those cached keys for all O(n log n) comparisons. This is much cheaper than a comparator function that gets called O(n log n) times. Interviewers who know their Python internals will appreciat