Hello, Super pythonists! Welcome to the python coding challenges. This is the best python coding interview question you had ever seen.
If you attend the top companies or coding challenges, definitely you will get this tricky coding challenge. so get ready to crack this coding challenge guys.
There are 100 doors, In the first iteration, we will open all doors and in the next iteration, we will close the doors. The instructions are given below.
No.of doors — 100 doors
1,2,3,4_,_________________100 – open
3,6,9_,__________________ 99 – open
and finally, calculate the below
#calculate No.of doors opened
#calculate No. of doors closed
Read more here:
How to generate QR code in python – Quick start guide
Coding challenge solution:
Trust me guys, python is the most simple and easy programming. it is just like the English alphabet. so I choose python to solve this coding challenge.
I simplified this solution in a very easy way, you can easily understand this python coding challenge solution.
c = 'close' p ='open' j =1 l = 1 n=100 # No.of Rooms open_door = 0 close_door =0 while l<n: for i in range(0,n+1,j): if i==0: print('Iteration:',j) continue if j%2!=0 : print(i,p) open_door +=1 else: print(i,c) close_door +=1 print('') l+=1 j+=1 if n%2==0: print('No.of Doors opened:',open_door) print('No.of Doors Closed:',close_door+1) else: print('No.of Doors opened:',open_door+1) print('No.of Doors Closed:',close_door)
let me explain to you how I cracked this coding challenge. I create 2 variables for open and close the doors. Now i took “n” as no.of rooms. I used while loop for iteration rooms.
range(0,10,2) : This function give all the even numbers. the output like below.
>>>list(range(0,10,2)) Here, # 0 - start index # 10 - end index # 2 - step size Output: [0, 2, 4, 6, 8]
if, else condition blocks are just simple, you can understand easily.
I add another if-else condition out of the while loop because using the range function we iterate (j-1) iterations.
If no.of rooms are even then we increase the close_door with 1 at last. and for odd no.of rooms, we add 1 to open_door.
if n%2==0: print('No.of Doors opened:',open_door) print('No.of Doors Closed:',close_door+1) else: print('No.of Doors opened:',open_door+1) print('No.of Doors Closed:',close_door)
I cracked this coding challenge in python in a very easy way, I hope you definitely love my code. Thank you for visiting us.
For more interview questions in python and coding challenges please subscribe to our newsletter below.