Second largest — Python Technical Interview Questions & Answers

Second largest

Second largest

  • Second largest is a classic practice problem. State the rule, trace a tiny input, then code. Fibonacci: next = sum of last two. Second largest: watch duplicates. Hanoi: move n-1, biggest, n-1. Distance: sqrt((x2-x1)**2 + (y2-y1)**2).
  • For Second largest: say the output for one small n before you write the loop. That is the exam.
  • Second largest — output: 8. Unique sorted → 1,3,8,9. Second last is 8, not 9 again.

Technical Interview Tip: Rule + one dry-run + then code.

Code Examples for Second largest

Example

# Second largest
a = [9, 1, 9, 8, 3]
u = sorted(set(a))
print(u[-2])

Second largest — output: 8. Unique sorted → 1,3,8,9. Second last is 8, not 9 again.

How to Explain Second largest to an Interviewer

Explain Second largest as if you are teaching a junior — definition, then one tiny script.

Second largest — know the rule. Second largest — tiny trace first.

What does the example print, and why?

Second largest — output: 8

What mistake do freshers make with Second largest?

Second largest — edge cases (n=0, duplicates).