Python Interview Prep: Questionnaire Guide Part 2

Pallavi Padav
Women in Technology
4 min readApr 20, 2024
https://www.theforage.com/blog/interview-questions/coding-interview-questions

Welcome to the interview preparation guide part 2. I have covered some of the Python coding FAQ in part 1, here is the collection of yet another set of frequently asked Python coding interview questions that will ace your interview.

Request you to solve these questionnaires on your own and then compare the logic. I would appreciate your sharing any other solutions to these questions in the comment section.

Q1. Write a code to reverse a string.

str = "Q1. Write a code to reverse a string."
rev_str = str[::-1]
rev_str

o/p: ‘.gnirts\xa0a esrever ot edoc a etirW .1Q’

Q2. Write a code snippet to sort a list.

lst = [4,2,0,1,5]
lst.sort()
lst

o/p: [0, 1, 2, 4, 5]

lst_c = ['a','m','y','i','b','d','A']
lst_c.sort()
lst_c

o/p: [‘A’, ‘a’, ‘b’, ‘d’, ‘i’, ‘m’, ‘y’]

Q3 Write a code to Reverse Words in a Sentence.

def reverse_words(s):
word = s.split()
reversed_sentence = ' '.join(reversed(word))
return reversed_sentence

str = "Python is awesome"
print(reverse_words(str))

o/p: awesome is Python

Q4 Explain the below code.

s1 = 'abc'
s2 = '123'
print(s1.join(s2))

o/p: 1abc2abc3

s1.join(s2) means that each character in s1 will be inserted between each character in s2. Since s2 is ‘123’, and s1 is ‘abc’, the result will be ‘1abc2abc3’

Q5 Write a program to check whether the i/p is digit or not.

str = input("Enter a string")
[print("Its a digit") if str.isdigit() else print("Its not a digit")]

o/p: Enter a string 123

Its a digit

Q6 Write a program to Randomize the Items of a List.

from random import shuffle
list_ip = ['Randomizing', 'the', 'Items', 'of', 'a', 'List']
shuffle(list_ip)
list_ip

o/p: [‘of’, ‘the’, ‘List’, ‘Randomizing’, ‘Items’, ‘a’]

Q7 Write a program to remove whitespace from a string.

Solution 1

str = "Write a program to remove whitespace from a string"
str = str.replace(' ','')
str

Solution 2

str = "Write a program to remove whitespace from a string"
str = re.sub(' ', '', str)
str

o/p: ‘Writeaprogramtoremovewhitespacefromastring’

Q8 Write a code to count Digits, Letters, and Spaces in a String.

str = "Congrats for securing 99% @ 12th class "
import re
digit_count = re.sub('[^0-9]','',str)
print(" Number of digits: {}. Digit present in the str :{}".format(len(digit_count), digit_count))

o/p: Number of digits: 4. Digit present in the str :9912

letter_count = re.sub('[^a-zA-Z]','',str)
print(" Number of letters: {}. letter present in the str :{}".format(len(letter_count), letter_count))

o/p: Number of letters: 26. letter present in the str :Congratsforsecuringthclass

Q9 Write a program to count White Spaces in a String.

str = "Write a program to count White Spaces in a String"
str.count(' ')

o/p: 9

Q10. Write a code to concatenate two arrays.

Solution 1

import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
concatenated_array = np.concatenate((array1, array2))
concatenated_array

o/p: array([1, 2, 3, 4, 5, 6])

Solution 2

from array import array

array1 = array('i', [1, 2, 3]) # 'i' represents integer type
array2 = array('i', [4, 5, 6])

# Using extend() method
array1.extend(array2)
array1

o/p: array(‘i’, [1, 2, 3, 4, 5, 6])

Q11. What are the different ways of deleting an element from a list?

lst = ['a', 2,4,5,7,'f',6.1, 'r']
lst.remove(6.1)
lst

o/p: [‘a’, 2, 4, 5, 7, ‘f’, ‘r’]

lst = ['a', 2,4,5,7,'f',6.1, 'r']
lst.pop(6)
lst

o/p: [‘a’, 2, 4, 5, 7, ‘f’, ‘r’]

Q12. Write a code to delete a list.

lst = ['a', 2,4,5,7,'f',6.1, 'r']
lst.clear()
lst

o/p: []

Q13 Write a code to check for Palindrome.

a = input("Enter a string")
if a == a[::-1]:
print("Entered string is Palindrome")
else:
print("Entered string is not Palindrome")

o/p: Enter a string radar

Entered string is Palindrome

Q14. Write a code snippet to concatenate lists.

Solution 1

List1= ["Wri", "a", "co","t", "concat", "li"]
List2 = ["te", " ","de","o", "enate","sts"]
list_1_2 = []
if len(List1) == len(List2):
for i in range(len(List1)):
list_1_2.insert(i, List1[i] + List2[i])
list_1_2

o/p: [‘Write’, ‘a ‘, ‘code’, ‘to’, ‘concatenate’, ‘lists’]

Solution 2

lst3 = [x + y for x, y in zip(List1, List2)]
print(lst3)

o/p: [‘Write’, ‘a ‘, ‘code’, ‘to’, ‘concatenate’, ‘lists’]

Q15. Write a code to get the indices of n maximum values in a given array.

def indices_of_n_max_values(arr, n):
# Get indices that would sort the array in ascending order
sorted_indices = np.argsort(arr)#The argsort() method returns the indices that would sort an array.

# Select the last n indices to get indices of n maximum values
max_indices = sorted_indices[-n:]

return max_indices

# Example usage:
arr = np.array([1, 3, 5, 2, 4, 10,7,9])
n = 3
max_indices = indices_of_n_max_values(arr, n)
print("Indices of", n, "maximum values:", max_indices)

o/p: Indices of 3 maximum values: [6 7 5]

Similar reads:

Python Interview Prep: Questionnaire Guide Part 1

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!!!!

--

--