Python Interview Prep: Questionnaire Guide Part 1

Pallavi Padav
Women in Technology
4 min readApr 18, 2024
https://www.filtered.ai/blog/online-coding-interview-tool-fd

Welcome to the interview preparation guide part 1. Python is one of the widely used programming languages in Data Science. Most of the ML or AI interview starts with Python coding. Here is the collection of the most frequently asked Python coding interview questions that will ace your interview.

Try solving these questionnaires on your own and then compare the logic.

Note: Some of the questions have more than one solution. I would appreciate your sharing any other solutions to these questions in the comment section.

Q1. Write a code snippet to check whether two Strings are Anagram?

Note: An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. Eg “secure” is an anagram of “rescue.”

Solution 1

str1 = "heart"
str2 = "earth"
from collections import Counter
def check(s1, s2):

if(Counter(s1) == Counter(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
check(str1, str2)

o/p: The strings are anagrams.

Solution 2

if sorted(str1) == sorted(str2):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")

o/p: The strings are anagrams.

Q2. Write a code to convert a string to a list.

Solution 1

str = " Write a code to convert a string to a list"
lst = str.split(' ')
lst, len(lst)

o/p: ([‘ ‘ , ‘Write’, ‘a’, ‘code’, ‘to’, ‘convert’, ‘a’, ‘string’, ‘to’, ‘a’, ‘list’],
11)

Note: Lets remove the space in front of ‘Write’

x = ''
if x in lst:
lst.remove(x)
lst

o/p: [‘Write’,
‘a’,
‘code’,
‘snippet’,
‘to’,
‘convert’,
‘a’,
‘string’,
‘to’,
‘a’,
‘list’]

Solution 2

str = " Write a code to convert a string to a list"
str = str.strip()
print(str)
lst = str.split(' ')
lst

o/p: Write a code to convert a string to a list

[‘Write’, ‘a’, ‘code’, ‘to’, ‘convert’, ‘a’, ‘string’, ‘to’, ‘a’, ‘list’]

Solution 3:

str = " Write a code to convert a string to a list"
str = str.replace(' W','W')
print(str)
lst = str.split(' ')
lst

Q3 Write a program to calculate the median using the NumPy arrays.

arr = np.array([6,3,9,7,7,2,1,0]) # 0,1,2,3,6,7,7,9
median = np.percentile(arr, 50)
median

o/p: 4.5

Q4 Write a code to find mode in a list of numbers.

Solution 1

list_ip = [9,6,2,5,1,7,8,9,9,6,1]
ip = set(list_ip)
max_ip = 1
mode = 0
for i in ip:
if list_ip.count(i) > max_ip:
max_ip = list_ip.count(i)
mode = i
print("Mode: {}, Count :{}".format(mode, max_ip))

o/p: Mode: 9, Count :3

Solution 2

max(set(list_ip), key = list_ip.count)

o/p: 9

Q5 Write a program to count Special Characters in a string.

import string

str = "hello world !! @ & ( )"
count = 0
for i in str:
if i in string.punctuation:
count += 1
print(count)

o/p : 6

Q6 Writing code to print the Fibonacci Series for a given number.

a = input("Enter a positive number")
# Fn = Fn-1 + Fn-2
# with seed values : F0 = 0 and F1 = 1.
def fibonacci_gen(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return fibonacci_gen(x-1) + fibonacci_gen(x-2)
lst = []
for i in range(int(a)+1):
lst.insert(i,fibonacci_gen(i) )

lst

o/p : Enter a positive number 8

[0, 1, 1, 2, 3, 5, 8, 13, 21]

Q7 How to count the number of occurrences of a character in a string.

str = 'characters in a string'
def char_counter(s):
dict_char = {}
for x in s:
if x in dict_char:
dict_char[x] += 1
else :
dict_char[x] = 1
return dict_char
char_counter(str)

o/p: {‘c’: 2,
‘h’: 1,
‘a’: 3,
‘r’: 3,
‘t’: 2,
‘e’: 1,
‘s’: 2,
‘ ‘: 3,
‘i’: 2,
’n’: 2,
‘g’: 1}

Q8 Write a code to count Vowels in a given Word.

vowel =  ['a', 'e', 'i', 'o', 'u']
str = "How to count Vowels in a given Word?"
count = 0
[count:= count+1 for i in str if i in vowel]
count

o/p: 11

Q9 Write a code to count Consonants in a given word.

vowel =  ['a', 'e', 'i', 'o', 'u']
str = "How to count Consonants in a given word?"
count = 0
[count:= count+1 for i in str if i not in vowel]
count

o/p: 28

Q10. Write a code to swap the values of two elements.

a , b = 10, 20
print("Before swapping : a = {}, b = {}".format(a,b))
b,a = a,b
print("After swapping : a = {}, b = {}".format(a,b))

o/p: Before swapping : a = 10, b = 20
After swapping : a = 20, b = 10

Q11. Write a program to check whether a number is prime or not.

a = input("Enter a number")
def prime_checker(x):
for i in range(2, x):
if x%i == 0 :
return 0
return 1
[print("Number is prime") if prime_checker(int(a)) else print("Number is not prime")]

o/p: Enter a number 1

Number is prime

Q12. Write a code to reverse an array.

Solution 1:

arr = ['v','e','q','r','g','z','a']
arr[::-1]

o/p: [‘a’, ‘z’, ‘g’, ‘r’, ‘q’, ‘e’, ‘v’]

Solution 2

arr = np.flip(arr)
arr

Q13. Write a code to convert a list of characters to a string of characters.

list_ip = ['w','r','i','t','e','a','c','o','d','e']
str = ','.join(list_ip)
str = str.replace(',','')
str

o/p: ‘writeacode’

Q14. Write a code to select only odd numbers using list comprehension.

list_ip = [5,6,2,8,1,9,10]
list_odd = [list_ip[i] for i in range(len(list_ip)) if list_ip[i]%2 != 0 ]
list_odd

o/p: [5, 1, 9]

Q15. Write a program in Python to return the factorial of a given number using recursion.

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

a = int(input("Enter a number"))
print(factorial(a))

o/p: Enter a number 5

120

Similar reads:

Python Interview Prep: Questionnaire Guide Part 2

Python Interview Prep: Questionnaire Guide Part 3

EndNote:

Thanks for reading the questionnaire, I hope enjoyed solving it. Please drop your suggestions or queries in the comment section.

Would love to catch you on Linkedin. Mail me here for any queries.

Happy coding!!!!

--

--