Fibonacci — Python Technical Interview Questions & Answers
Fibonacci
Fibonacci
- Fibonacci 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 Fibonacci: say the output for one small n before you write the loop. That is the exam.
- Fibonacci — output: [0, 1, 1, 2, 3, 5]. Real sequence, not dummy numbers.
Technical Interview Tip: Rule + one dry-run + then code.
Code Examples for Fibonacci
Example
# Fibonacci
a, b = 0, 1
out = []
for _ in range(6):
out.append(a)
a, b = b, a + b
print(out)
Fibonacci — output: [0, 1, 1, 2, 3, 5]. Real sequence, not dummy numbers.
How to Explain Fibonacci to an Interviewer
Explain Fibonacci as if you are teaching a junior — definition, then one tiny script.
Fibonacci — know the rule. Fibonacci — tiny trace first.
What does the example print, and why?
Fibonacci — output: [0, 1, 1, 2, 3, 5]
What mistake do freshers make with Fibonacci?
Fibonacci — edge cases (n=0, duplicates).