sorted() — Python Technical Interview Questions & Answers
sorted()
sorted()
- sorted() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- sorted() — output: [1, 2, 3] then ['A', 'a', 'h', 's']. New list, original unchanged.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix sorted up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call sorted() once and say what prints.
Code Examples for sorted()
Example
# sorted()
print(sorted([3, 1, 2]))
print(sorted("Asha"))
sorted() — output: [1, 2, 3] then ['A', 'a', 'h', 's']. New list, original unchanged.
How to Explain sorted() to an Interviewer
Explain sorted() as if you are teaching a junior — definition, then one tiny script.
sorted() is built-in — no import. One call + exact output.
What does the example print, and why?
sorted() — output: [1, 2, 3] then ['A', 'a', 'h', 's']
What mistake do freshers make with sorted()?
Wrong type → TypeError.