Asked Python interview questions for data science/engineering.

In this article, I present and share the solution for questions that were asked to me or I had asked in data science/engineering interviews.

shubham badaya
5 min readDec 29, 2023
Photo by Maranda Vandergriff on Unsplash

I’ll be updating this list frequently with new questions, so I suggest you save this article.I am not a python expert, so these solutions might be sub optimal.

1. How to print a tree pattern as shown below?

           * 
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
n = 10
for i in range(1,n+1):
print(' '*n , end = ' ')
print('* '*i )
n -=1

2. Write a function to the nth value in the Fibonacci series.

# Function for nth Fibonacci number
def Fibonacci(n):

# Check if input is 0 then it will
# print incorrect input
if n < 0:
print("Incorrect input")

# Check if n is 0
# then it will return 0
elif n == 0:
return 0

# Check if n is 1,2
# it will return 1
elif n == 1 or n == 2:
return 1

else:
return Fibonacci(n-1) + Fibonacci(n-2)
# Output
print(Fibonacci(9))

34

3. Given a list [ 2,7,1,4,5,3]. Write a Python program to implement a bubble sort algorithm.

lst = [ 2,7,1,4,5,3]

n = len(lst)

for j in range(n-1):
for i in range(n-1-j):
if lst[i] > lst[i+1]:
t = lst[i]
lst[i] = lst[i+1]
lst[i+1] = t
else:
pass
# Output
print(lst)
[1, 2, 3, 4, 5, 7]

4. Multindexing in pandas.

Given a data frame with an index as year and book, and column as author.

import pandas as pd
year = [1990,1991,1992,1993]
author = ['a','b','c','d']
t = ['maths', 'english', 'science' , 'maths']
df = pd.DataFrame({'year':year,'author':author,'book':t})
df.set_index(['year','book'], inplace = True)
print(df)

#Output
author
year book
1990 maths a
1991 english b
1992 science c
1993 maths d

Write Python code to subset data for all records where the book is maths.

# Output
df.loc[pd.IndexSlice[:, 'maths'],:]

Write Python code to subset data for all records where the book is maths and the year is 1990.

# Output
df.loc[pd.IndexSlice[[1990], ['maths']], :]

5. Given a list_of_list = [[1,2,3],[4,5,6],[7,8]]. convert it into a single list.

[y for x in list_of_list for y in x ]
# Output
[1, 2, 3, 4, 5, 6, 7, 8]

6. Write a Python program to check if 2 strings are anagrams.

Anagrams are strings that if rearranged will be equal i.e. count and number of characters are the same.

def anagram(s1,s2):
def get_dict(s):
y ={}
for i in s:
if i in y:
y[i] +=1
else:
y[i] =1
return(y)

return( get_dict(s1)==get_dict(s2))
# Output
anagram('word', 'wodr')

True

anagram('dog', 'dogg')

False

7. Write a Python program to print the reverse of a given string.

x = 'khokho'
print(x[::-1])
# Ouput 
ohkohk

8. Give a list arr = [1 , [2,3] ,[ [4,5]]] ,how you would flatter that out.


def flatten(arr):
res=[]
for i in arr:
if type(i)==list:
flatten(i)
else:
res.append(i)
return(res)
# Output
flatten(arr)

[1,2,3,4,5]

9. How do you print the first non-repeated character from a string?

text = 'aaccbbhhddifgghhsss'

dict = {}
# for each ele in text, add ele in dict
for ele in text:
try:
dict[ele] += 1
except:
dict[ele] = 1

# if frequency is more than 1
# print the element
for item in dict:
if(dict[item] == 1):
print(item)
break

10. Given an array, arr = [2,6,3,9,11]. Write a program to find all pairs of integer numbers whose sum is equal to a given number, n = 9.

def findpairs(arr, n):
lst =[]
for first_num in arr:
second_num = n - first_num
if second_num in arr:
if first_num != second_num:
lst.append((first_num, second_num))
return(lst)
# Output
findpairs(arr,n)
[(6, 3), (3, 6)]
# Input
arr = [2,7,11,15]
n = 9
findpairs(arr,n)

# Output
(2,7)

11. Given a list write a function to get the first, and second best scores from the list. The list might contain duplicates.

  1. Method 1 — using sort if duplicate values are allowed
mylist = [84,85,86,87,85,90,85,83,23,45,1,2,0]
mylist.sort(reverse=True)

2. Method 2 — if unique values are needed.

def firstSecond(given_list):
a = given_list #make a copy

a.sort(reverse=True)

print(a)

first = a[0]

second = None

for element in given_list:
if element != first:
second = element
return first, second
# Output
mylist = [84,85,86,87,85,90,85,83,23,45,1,2,0]
firstSecond(mylist)

[[90, 90, 87, 86, 85, 85, 84, 83, 45, 23, 2, 1, 0]]
(90, 87)

12. Write a program such that if a string is represented as ‘aaaaabbbccaa’ then the output of the function should be ‘5a3b2c2a’. This is used to shorten the string storage, in cases where there is huge data.

# given program is an example of run length encoding
s1 = 'aaaaabbbccaa'
s2 = ''


countConsecutive = 1

for i in range(len(s1)) :
# condition for last elements or previous element is not equal to ne
if( (i +1 > len(s1)-1) or (s1[i] != s1[i+1]) ):
s2 = s2 + str(countConsecutive) + s1[i]
countConsecutive = 1
# if all good then add 1
else:
countConsecutive += 1

print(s2)
# Output
5a3b2c2a

13. In an array find the second max value.

arr = [1,2,3,4,5]

mv = arr[0]
smv = arr[1]


for i in range(2, len(arr)):
if arr[i] > mv:
smv = mv
mv = arr[i]
else:
smv = max(arr[i] , smv)

14. In an ordered list of up to n integers. Find the missing elements.


input_lst = [1,2,3,7,9]
output_lst = []

# how many missing elements?
def missing_elements(input_lst):
len_lst = len(input_lst)
last_value = input_lst[len_lst-1]
for i in range(1,last_value):
if i not in input_lst:
output_lst.append(i)
return output_lst
# Output
missing_elements(input_lst)
[4,5,6,8]

Feel free to comment for any other solution to any problem.

--

--