PYTHON CODING INTERVIEW
PART-1 (Top Coding Questions)
Q1. Write a Python program to print "Hello World".
Answer
print("Hello World")
Q2. Write a program to add two numbers.
Answer
a = 10
b = 20
sum = a + b
print(sum)
Output
30
Q3. Write a program to swap two numbers.
Answer (Method 1)
a = 10
b = 20
a, b = b, a
print(a)
print(b)
Output
20
10
Answer (Method 2)
a = 10
b = 20
temp = a
a = b
b = temp
print(a)
print(b)
Q4. Write a program to find whether a number is even or odd.
Answer
num = 15
if num % 2 == 0:
print("Even")
else:
print("Odd")
Q5. Write a program to find the largest of two numbers.
Answer
a = 50
b = 80
if a > b:
print(a)
else:
print(b)
Q6. Write a program to find the largest of three numbers.
Answer
a = 50
b = 80
c = 70
if a > b and a > c:
print(a)
elif b > c:
print(b)
else:
print(c)
Q7. Write a program to calculate factorial.
Answer
num = 5
fact = 1
for i in range(1, num + 1):
fact = fact * i
print(fact)
Output
120
Q8. Write a program to print the multiplication table.
Answer
num = 5
for i in range(1,11):
print(num,"x",i,"=",num*i)
Output
5 x 1 = 5
...
5 x 10 = 50
Q9. Write a program to check Prime Number.
Answer
num = 17
count = 0
for i in range(1,num+1):
if num%i==0:
count+=1
if count==2:
print("Prime")
else:
print("Not Prime")
⭐ Interviewer Follow-up
Interviewer:
Can you optimize it?
Expected Answer
num = 17
if num > 1:
for i in range(2,int(num**0.5)+1):
if num%i==0:
print("Not Prime")
break
else:
print("Prime")
� Agar ye approach aata hai to interviewer par achha impression padta hai.
Q10. Write a program to print Fibonacci Series.
Answer
n = 10
a = 0
b = 1
for i in range(n):
print(a)
c = a+b
a=b
b=c
Output
0
1
1
2
3
5
8
13
21
34
⭐ Interview Tip
Interviewer sirf code nahi poochta.
Wo poochta hai:
Q. Explain your code.
Example:
Factorial Program.
Wo bolega:
Why did you write
fact = 1
Tumhara Answer hona chahiye:
"Because factorial multiplication starts from 1. If we initialize it with 0, the result will always be 0."
Ye explanation interview me bahut value add karti hai.
⭐ Common Fresher Mistake
Interviewer:
Write Fibonacci Series.
Candidate code likh deta hai.
Interviewer:
Can you explain the logic?
Candidate chup ho jata hai.
� Galat.
Har program ka logic explain karna bhi aana chahiye.