Status: 2021 Graduate
Position: Analyst in a Service Based Company
Location: India
Date: March 2022
Technical phone screen (1 hour):
Interviewer called 15 minutes late. He explained the order the interview would be conducted as highlighted in the following points.
Given a positive number, find how many steps it takes to converge to 1. For a even number, divide it by 2 and for any odd number multiply the number by 3 and increment by 1.
Example -
6 -> 3 -> 10 -> 5 ->16 -> 8 -> 4 -> 2 -> 1
Output - 9
- My approach and follow ups.
I gave a simple recursive solution. The interviewer asked me to do some recursive optimizations. He later explained that he wanted me to handle the testcases which may fail. So, I checked the negative numbers and came to the conclusion that they will never converge to 1. They will always converge to -1 and if handled, it may get stuck in an infinite loop (StackOverFlow Exception). So, I added a check for negative numbers and then for 0. We also had some discussion on what was the best value to be returned for inputs less than equal to 0.
Then, he asked if throwing an exception in place of returning -1 was a better option. I didn't know the internal working of throws keyword. But after giving it a thought, I said that if the function which I had just written was being used by other fuction, the compiler will check for -1 in all the cases. While in the other case, the control will jump to then catch block only when there is an exception. But he wanted a one-on-one comparision of throws and return. He later explained that throws keyword does a bit of memory allocation and hence return statement was a better option.
Given a string, return if it is balanced parenthesis or not.
Example -
((())) -> true
)))() -> false
-My approach and follow ups
I told him that this problem could be solved using stack data structure. After giving it a second thought, I explained him another solution which I thought might just work. I kept a counter for the number of open brackets and once I saw the closing brackets, I would decrement it by one. And, in any iteration, if the open goes below 0, we know that the brackets are not balanced and I could return false straightaway. I think I already ruined the follow-up which might have been to do the checking without stack. But at that time, I was a bit nervous and so went with what I thought would be less complicated.
The interviewer then included the curly braces also. I approached it in the same way but now with two variables for two types of braces.
and then adding the exit condition. But, unfortunately the solution was not correct. I mean it would fail for a few testcases. The interviewer then said that because I appraoched both the questions in the same way, I was not able to get to the solution. Since we were running out of time(last 10 minutes), he quickly asked me to explain how the solution would look like when we use a stack. I explained. I quickly implemented it using stack when I was asked to do it. Then, I was asked about the time and space complexity.
Next Day, I got the email for rejection.
Miscellaneous (My lackings)
I know it might not be the best experience you would be looking for. But, if you have made it this far, my heartfelt thanks to you.