Learn Python With Dhawal - 15 - Interview Coding Questions

Here are some questions Interview Coding Questions which can be solved by very basic knowledge. Try out first on your own and then check the solutions. Downloadable files can be found in my github repository and there will be a few more things up in my github soon, if you have an account you can for sure fork it for future reference or just make a branch and contribute to it.

Q1 Consider the Input String as 'aaabbccccaa'. Find the number of each alphabet in the string. The output should be 'a3b2c4a2'.

Solution : First we will create an input string variable and convert it into list. Then in second step we set up a few needed variables like counter and list to save out output. Next we create a for loop to run for the full length of the Input String and check the number of each alphabet. When the alphabet changes we reset the counter to 1 and count again and repeat till the string end is reached. During each reset we append the counter value into the output list.

print("The Input we have is : ",input_list)
for i in range(1,len(input_list)):

Q2 Have the function ABCheck(str) take the str parameter being passed and return the string true if the characters a and b are separated by exactly 3 places anywhere in the string at least once (ie. "lane borrowed" would result in true because there is exactly three characters between a and b). Otherwise return the string false.
Sample Test Cases

Input:"after badly"


Input:"Laura sobs"


Solution : Here we first take the Input String and then remove all the blank spaces. Then there are two options, we can either convert the string into list and then find the count by using numpy array or we can just use basic method of travelling a string array. While we are traversing the string, we check if there's 'a' or 'b', if found we append their indexes in a list. There will be a seperate list for both a and b, as we finish the traversal we will have all indexes of a and b in the list. We can then run a for loop to find if any one index of the a and b has a difference of 3 between them. If the answer is yes, we show the output as true else false.
str=str.replace(" ","")
print (str)


for i in range(len(str)):

print "a is",a
print "b is",b

for i in a:
 for j in b:

if (counter==0):

Q3 Have the function ThreeFiveMultiples(num) return the sum of all the multiples of 3 and 5 that are below num. For example: if num is 10, the multiples of 3 and 5 that are below 10 are 3, 5, 6, and 9, and adding them up you get 23, so your program should return 23. The integer being passed will be between 1 and 100.
Sample Test Cases





Solution :  Here we first take the input from user and convert it into 'int' as the input method takes everything as a string by default. Then we check if the given input is what we expect it to be. Next we create a loop and just run it range till input+1 then we just inside the loop keep adding the number by checking if the number is divisible by 3 or 5 using modulus division.  At the end we just print the sum.

num=int(input("Enter a number between 1 and 100\n"))

if (num>100 or num<0):

 print("enter a valid number")



 for i in range(num+1):

  if (i%3==0 or i%5==0):



With this I hope the you can now take on the simple problems and try working out solutions for them on your own. Next we will learn about Error handling in python. It can be quite fun if you understand the concept. It's quite simple to be honest.



This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.


Post a Comment

Do comment and share your thoughts.