PYTHON CODING INTERVIEW
PART-2
Q11. Write a Python program to reverse a string.
Answer (Method 1)
text = "Python"
print(text[::-1])
Output
nohtyP
Answer (Method 2)
text = "Python"
reverse = ""
for i in text:
reverse = i + reverse
print(reverse)
⭐ Interviewer Follow-up
Q: Which method is better?
Answer:
The slicing method ([::-1]) is shorter and commonly used in Python.
The loop method helps demonstrate the logic of reversing a string and is useful in interviews.
Q12. Write a program to check whether a string is a palindrome.
Answer
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output
Palindrome
Q13. Write a program to count vowels.
Answer
text = "Data Science"
count = 0
for i in [Link]():
if i in "aeiou":
count += 1
print(count)
Output
Q14. Write a program to count consonants.
Answer
text = "Python"
count = 0
for i in [Link]():
if [Link]() and i not in "aeiou":
count += 1
print(count)
Output
Q15. Write a program to remove duplicate characters from a string.
Answer
text = "programming"
result = ""
for i in text:
if i not in result:
result += i
print(result)
Output
progamin
Q16. Write a program to find the largest element in a list.
Answer
numbers = [10,45,22,90,15]
largest = numbers[0]
for i in numbers:
if i > largest:
largest = i
print(largest)
Output
90
⭐ Interviewer Follow-up
Q: Can you do it using a built-in function?
Answer
numbers = [10,45,22,90,15]
print(max(numbers))
Q17. Write a program to find the smallest element.
Answer
numbers = [10,45,22,90,15]
smallest = numbers[0]
for i in numbers:
if i < smallest:
smallest = i
print(smallest)
Output
10
Q18. Write a program to find the second largest element.
Answer
numbers = [10,45,22,90,15]
[Link]()
print(numbers[-2])
Output
45
⭐ Interviewer Follow-up
Q: Can you do it without sorting?
� Ye advanced question hai. Freshers se kabhi-kabhi poocha jata hai, lekin agar nahi aata to panic mat hona.
Q19. Write a program to remove duplicates from a list.
Answer
numbers = [10,20,20,30,40,40,50]
unique = []
for i in numbers:
if i not in unique:
[Link](i)
print(unique)
Output
[10,20,30,40,50]
Easy Method
numbers = [10,20,20,30,40,40,50]
print(list(set(numbers)))
� Interview me pehle loop wala logic batao. Fir bolo:
"Python me set() ka use karke bhi duplicates remove kar sakte hain."
Ye answer interviewer ko pasand aata hai.
Q20. Write a program to sort a list.
Ascending Order
numbers = [50,10,40,30]
[Link]()
print(numbers)
Output
[10,30,40,50]
Descending Order
numbers = [50,10,40,30]
[Link](reverse=True)
print(numbers)
Output
[50,40,30,10]
⭐ Interviewer Follow-up
Q: What is the difference between sort() and sorted()?
Answer
sort():
Modifies the original list.
Returns None.
sorted():
Returns a new sorted list.
Does not modify the original list.
Example:
numbers = [3, 1, 2]
print(sorted(numbers)) # [1, 2, 3]
print(numbers) # [3, 1, 2]
[Link]()
print(numbers) # [1, 2, 3]
⭐ Most Common Mistake
Interviewer:
How do you remove duplicates?
Candidate:
list(set(numbers))
Interviewer:
Can you do it without using set?
Candidate:
�
� Isi liye main har question me basic logic + Python shortcut dono sikha raha hoon.
Ab tak humne cover kiya:
Theory
40 Python Interview Questions
Coding
20 Python Programs