0% found this document useful (0 votes)
5 views5 pages

Python Interview Questions & Answers

The document provides a comprehensive list of Python interview questions and answers categorized into easy, medium, hard, and OOP sections. Each section contains various coding problems along with their solutions, covering topics such as string manipulation, data structures, algorithms, and object-oriented programming concepts. This resource serves as a useful guide for candidates preparing for Python interviews.

Uploaded by

Naveengovindaraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Python Interview Questions & Answers

The document provides a comprehensive list of Python interview questions and answers categorized into easy, medium, hard, and OOP sections. Each section contains various coding problems along with their solutions, covering topics such as string manipulation, data structures, algorithms, and object-oriented programming concepts. This resource serves as a useful guide for candidates preparing for Python interviews.

Uploaded by

Naveengovindaraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON INTERVIEW — QUESTIONS WITH ANSWERS (EASY + MEDIUM + HARD + OOP)

--------------------------------------------------------------
SECTION 1 — EASY CODING QUESTIONS WITH ANSWERS
--------------------------------------------------------------

1. Reverse a string
Answer:
s = "python"
print(s[::-1])

2. Check palindrome
Answer:
s = "madam"
print(s == s[::-1])

3. Count vowels in string


Answer:
v = sum(1 for c in "hello" if c in "aeiou")
print(v)

4. Largest of 3 numbers
Answer:
print(max(10, 20, 5))

5. Swap without temp


Answer:
a, b = b, a

6. Factorial
Answer:
f=1
for i in range(1,n+1): f*=i

7. Sum of digits
Answer:
print(sum(int(d) for d in str(123)))

8. Even/Odd
Answer:
print("Even" if n%2==0 else "Odd")

9. Remove duplicates
Answer:
print(list(set([1,2,2,3])))

10. Fibonacci
Answer:
a,b=0,1
for i in range(n):
print(a)
a,b=b,a+b)

--------------------------------------------------------------
SECTION 2 — MEDIUM QUESTIONS WITH ANSWERS
--------------------------------------------------------------

1. Second largest in list


Answer:
arr=[10,30,20]
[Link]()
print(arr[-2])

2. Check anagram
Answer:
print(sorted("silent")==sorted("listen"))

3. Word count
Answer:
from collections import Counter
print(Counter("python is easy python".split()))

4. Armstrong number
Answer:
n=153
s=sum(int(d)**3 for d in str(n))
print(n==s)

5. Prime numbers in range


Answer:
for n in range(2,50):
for i in range(2,int(n**0.5)+1):
if n%i==0:break
else: print(n)

6. Missing number 1–n


Answer:
nums=[1,2,4,5]
n=5
print(n*(n+1)//2 - sum(nums))

7. Binary search
Answer:
def bs(arr,x):
l,r=0,len(arr)-1
while l[=r:
m=(l+r)//2
if arr[m]==x:return m
elif arr[m][x:l=m+1
else:r=m-1
return -1

8. Reverse words
Answer:
s="hello world"
print(" ".join(word[::-1] for word in [Link]()))

--------------------------------------------------------------
SECTION 3 — HARD QUESTIONS WITH ANSWERS
--------------------------------------------------------------

1. Reverse linked list logic


Answer:
prev=None
while head:
nxt=[Link]
[Link]=prev
prev=head
head=nxt

2. Quicksort logic
Answer:
def qs(a):
if len(a)[=1:return a
p=a[0]
left=[x for x in a[1:] if x[p]
right=[x for x in a[1:] if x]=p]
return qs(left)+[p]+qs(right)
3. Detect loop (Floyd)
Answer:
slow=fast=head
while fast and [Link]:
slow=[Link]
fast=[Link]
if slow==fast: print("Loop")

4. LRU Cache logic


Answer:
Use OrderedDict.

--------------------------------------------------------------
SECTION 4 — OOP QUESTIONS WITH ANSWERS
--------------------------------------------------------------

1. What is OOP?
Answer: Object-Oriented Programming uses classes and objects to structure code.

2. What is a class?
Answer: A blueprint for creating objects.

3. What is an object?
Answer: Instance of a class.

4. What is __init__?
Answer: Constructor method that initializes object variables.

5. What is inheritance?
Answer: When one class derives properties of another.

6. Polymorphism?
Answer: Same method name, different behaviors.

7. Encapsulation?
Answer: Restricting access to data using private variables.

8. Abstraction?
Answer: Showing essential features, hiding internal details.

9. What is super()?
Answer: Method to access parent class methods.

10. What is MRO?


Answer: Order in which Python resolves methods in multiple inheritance.

--------------------------------------------------------------
END OF DOCUMENT
--------------------------------------------------------------

You might also like