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.

input_string="aaabbccccaaa" input_list=list(input_string) counter=1 character_list=[] print("The Input we have is : ",input_list) character_list.append(input_list[0]) for i in range(1,len(input_list)): if(input_list[i-1]==input_list[i]): counter=counter+1 else: character_list.append(counter) counter=1 character_list.append(input_list[i]) character_list.append(counter) print(character_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"**

**Output:false**

**Input:"Laura sobs"**

**Output:true**

**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=input() str=str.replace(" ","") print (str) a=[] b=[] counter=0 for i in range(len(str)): if(str[i]=='a'): a.append(i) if(str[i]=='b'): b.append(i) print "a is",a print "b is",b for i in a: for j in b: if(abs(i-j)==3): counter=counter+1 if (counter==0): print("false") else: print("true")

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

**Input:6**

**Output:8**

**Input:1**

**Output:0**

**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") else: sum=0 for i in range(num+1): if (i%3==0 or i%5==0): sum=sum+i print(sum)

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.

**<< PREVIOUS CHAPTER || NEXT CHAPTER >> --INDEX--**

**~*~*~*~**

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

## 0 comments:

## Post a Comment

Do comment and share your thoughts.