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.

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

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

~*~*~*~

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.

Share :

How to run GUI apps on Windows Subsystem for Linux - WSL

I have been a fan of the linux bash terminal ever since I discovered how OP can it get. I desperately wished the windows cousin of the terminal aka command prompt or the new found powershell was that capable. Although I loved how several things turned out to be awesome and I so wanted to switch to linux distributions but only one annoying thing stopped me, MS Office. The day MS Office or something similarly capable gets onto the linux side of things, I'd surely dive in to switch. But until then, I'd love the little flavour of Linux in the form of WSL on the windows I've already got in my hands.

The WSL is kinda like the terminal only OS version of the Linux distributions that they are offering currently. But I installed the Kali and Ubuntu versions and they look pretty good to me. I wanted to somehow run GUI based apps on the system. There are thing which are made for linux and which just run seamlessly and in their best form on the Linux, hence I was looking for the way to run GUI apps like your everyday gedit or leafpad or something heavy like umbrello, on the WSL. After looking into the depths of Internet, I finally found a pretty easy to do work around for this adventure.

So, without further adieu, let's begin.

Instructions.

1. Download VcXsrv from the sourceforge website. It's an open source software. What it basically does is, it hosts the display output from a remote server. It's based on a very old Linux protocol to perform remote computing.
2. Install it using either the Default options or Choose whatever you feel like choosing. I opted for multiple windows and installed it normally with everything else.
3. Next, open the WSL distribution that you've installed and type 'export DISPLAY=127.0.0.1:0' or you can use ':1' if it doesn't work. But usually it works with the before said option.
4. Now, say you want to run gedit and it is installed in your WSL. Then type 'gedit' in the terminal and just wait for the output on your Windows side.
5. Make sure the X server is running before you try opening any app from the linux terminal.
Here what happens is that, the windows runs an X server. The server takes the display output which is hosted on the local host aka the IP that we have used aka on your own machine. Also ensure that you allow the X server app to run the private networks at least. Also the access control can be enabled and disabled depending on how you plan to use this work around.  You can not only run linux apps from your WSL but if you say have a remote Raspberry Pie running as a remote server you can also get the display output from it using SSH connection. Since we are running on a localhost here, we don't need to have an SSH connection, as its not over a network connection and probably way faster than getting any output from remote server connected with network connection.

If you are wondering how to enable and use WSL, first you need to enable it in the windows features. You can find the full detailed tutorial about the same here from the official Microsoft website.

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.