0% found this document useful (0 votes)
7 views7 pages

Python Interview Questions Extended

The document contains 45 Python coding interview questions along with their solutions. Topics covered include string manipulation, number theory, data structures, and algorithms. Each question is accompanied by a concise code snippet demonstrating the solution.

Uploaded by

Maria Fatima
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)
7 views7 pages

Python Interview Questions Extended

The document contains 45 Python coding interview questions along with their solutions. Topics covered include string manipulation, number theory, data structures, and algorithms. Each question is accompanied by a concise code snippet demonstrating the solution.

Uploaded by

Maria Fatima
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 Coding Questions (45 with

Solutions)

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

2. Check if a string is palindrome


s = "madam"
print(s == s[::-1])

3. Count vowels and consonants


def count_vowels_consonants(text):
vowels = "aeiouAEIOU"
v = c = 0
for ch in text:
if ch.isalpha():
if ch in vowels:
v += 1
else:
c += 1
return v, c

print(count_vowels_consonants("Hello World"))

4. Factorial of a number (recursion)


def factorial(n):
return 1 if n == 0 else n * factorial(n-1)

print(factorial(5))

5. Check if number is prime


num = 29
is_prime = num > 1 and all(num % i != 0 for i in range(2, int(num**0.5)+1))
print(is_prime)

6. Fibonacci sequence
n = 10
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a+b

7. Largest element in a list


arr = [10, 25, 7, 98, 56]
print(max(arr))

8. Second largest element in a list


arr = [10, 25, 7, 98, 56]
arr = list(set(arr))
arr.sort()
print(arr[-2])

9. Check Armstrong number


num = 153
order = len(str(num))
print(num == sum(int(d)**order for d in str(num)))

10. Reverse a number


num = 12345
rev = int(str(num)[::-1])
print(rev)

11. Sum of digits


num = 12345
print(sum(int(d) for d in str(num)))

12. Find GCD


import math
print(math.gcd(54, 24))

13. Find LCM


import math
a, b = 12, 15
print(abs(a*b) // math.gcd(a, b))

14. Check leap year


year = 2024
print(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0))

15. Swap numbers


a, b = 5, 10
a, b = b, a
print(a, b)

16. Remove duplicates from list


arr = [1,2,2,3,4,4,5]
print(list(set(arr)))

17. Sort list manually


arr = [64,25,12,22,11]
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if arr[i]>arr[j]:
arr[i],arr[j]=arr[j],arr[i]
print(arr)

18. Find missing number


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

19. Check anagrams


s1, s2 = "listen", "silent"
print(sorted(s1) == sorted(s2))

20. Character frequency


from collections import Counter
s="programming"
print(Counter(s))

21. Check rotations


s1, s2 = "abcde", "deabc"
print(len(s1)==len(s2) and s2 in (s1+s1))

22. Common elements


a=[1,2,3,4]
b=[3,4,5,6]
print(list(set(a)&set(b)))

23. Unique elements


a=[1,2,3,4]
b=[3,4,5,6]
print(list(set(a)^set(b)))

24. Check pangram


import string
s="The quick brown fox jumps over the lazy dog"
print(set(string.ascii_lowercase).issubset(set(s.lower())))

25. Reverse words in sentence


s="I love Python"
print(" ".join(s.split()[::-1]))

26. Factorial loop


n=5
fact=1
for i in range(1,n+1): fact*=i
print(fact)

27. Check list palindrome


arr=[1,2,3,2,1]
print(arr==arr[::-1])

28. Largest word


s="Python is an amazing language"
print(max(s.split(), key=len))

29. Count words


s="I love Python programming"
print(len(s.split()))

30. Check even/odd without %


num=7
print("Even" if (num&1)==0 else "Odd")

31. Longest substring no repeat


s="abcabcbb"
longest="";temp=""
for ch in s:
if ch in temp:
temp=temp[temp.index(ch)+1:]
temp+=ch
if len(temp)>len(longest): longest=temp
print(longest)

32. First non-repeating char


s="aabbcde"
for ch in s:
if s.count(ch)==1:
print(ch);break

33. Valid palindrome (ignore punctuation)


import re
s="A man, a plan, a canal: Panama"
clean=re.sub(r"[^A-Za-z0-9]","",s).lower()
print(clean==clean[::-1])

34. Intersection of arrays


a=[1,2,4,5,6]; b=[2,3,5,7]
i=j=0; result=[]
while i<len(a) and j<len(b):
if a[i]==b[j]: result.append(a[i]); i+=1; j+=1
elif a[i]<b[j]: i+=1
else: j+=1
print(result)

35. Binary search


def binary_search(arr,target):
low,high=0,len(arr)-1
while low<=high:
mid=(low+high)//2
if arr[mid]==target: return mid
elif arr[mid]<target: low=mid+1
else: high=mid-1
return -1
print(binary_search([1,3,5,7,9],7))
36. Find duplicates in list
arr=[1,2,3,2,4,5,1]
print([x for x in set(arr) if arr.count(x)>1])

37. Flatten nested list


def flatten(lst):
res=[]
for i in lst:
if isinstance(i,list): res.extend(flatten(i))
else: res.append(i)
return res
print(flatten([1,[2,[3,4],5],6]))

38. Majority element


arr=[2,2,1,1,2,2,2]
print(max(set(arr), key=arr.count))

39. Rotate list


arr=[1,2,3,4,5]
k=2;k%=len(arr)
print(arr[-k:]+arr[:-k])

40. Stack with list


stack=[]
stack.append(10)
stack.append(20)
print(stack.pop())
print(stack)

41. Queue with deque


from collections import deque
q=deque()
q.append(1);q.append(2)
print(q.popleft())

42. Second most frequent char


from collections import Counter
s="successes"
freq=Counter(s)
print(freq.most_common(2)[1])
43. Pascal triangle
def pascal(n):
tri=[[1]]
for _ in range(n-1):
prev=tri[-1]
row=[1]+[prev[i]+prev[i+1] for i in range(len(prev)-1)]+[1]
tri.append(row)
return tri
print(pascal(5))

44. Transpose matrix


matrix=[[1,2,3],[4,5,6]]
print(list(map(list,zip(*matrix))))

45. Check isomorphic strings


def is_isomorphic(s,t):
return len(set(zip(s,t)))==len(set(s))==len(set(t))
print(is_isomorphic("egg","add"))

You might also like