Facebook Interviews-2

I had been waiting anxiously and nervously for the first telephonic round of Facebook. Finally, my phone rang a few minutes later than the scheduled interview time and I received the call. She apologized for the slight delay in calling and asked if it was still a good time to continue the interview. After I said yes, she introduced herself and said a few lines about the team she worked in. I neither remember the name of female interviewer nor the team she mentioned, perhaps because of the anxiety. She asked me to create a document on collabedit.com and share the key for that document. Once, she confirmed that she could see the document, she went ahead to ask me a coding question.

The question was:

You have to write a function which takes as input two sorted arrays A and B each containing n elements. Array A can hold upto 2*n elements where as Array B can hold upto n elements. Merge the elements of array A and array B into array A such that it is sorted. You can’t use any additional data structure, you may only use extra constant memory space.

After thinking for about it for a while, I dived straight into writing the code in the online document shared with her. She asked me to elaborate what I was thinking before getting down to complete the code. I explained to her the algorithm, she was satisfied and asked me to go on and complete the code. After seeing my code, she said that it seems right. She then asked me what are the test cases that I would use to test before pushing this piece of code to production. I suggested testing this code with a few corner cases such as when the last element of array B is smaller than first element of array A and vice-versa, and a case when array A and array B were identical. She was satisfied with my response to this question. She then asked me another question.

The next question was:

You have to write a function checkRegex() which takes two strings as input, one string represents a regex expression and other is an input string to match with the regex expression passed as the other parameter. Return true if it matches, else false.
Regex may contain characters ‘a-z’, ‘.’ and ‘*’ where ‘.’ matches any character and ‘*’ means 0 or more occurrences of the previous character preceding it.

Examples:
1) a*b matches b,ab,aab
2) a.b matches aab, abb, acb,…, azb
3) .* matches all the valid strings formed by using the lowercase letters

This question was a bit tough to solve. I observed a few sample inputs and the corresponding regex patterns and started writing the code which would have worked for them but wasn’t generic enough to suit especially the case when ‘*’ was present in the regex pattern. After hitting my head against the wall for quite some time trying to solve it, the interviewer helped me by suggesting to think if I could solve this question using recursion. I started thinking on those lines and was able to come up with a recursive formula which for a specific example would read something like this:

checkRegex("a*b","aaab") = checkRegex("b","aaab") OR checkRegex("a*b","aab")

I conveyed this idea to her and how I could incorporate this idea into code. She said that I was on the right track but as time has ran out, she would have to end up this conversation then. I asked if I need to mail the code for this question to her after a few minutes. She said that it’s not needed as you were proceeding on the right track.

I was not sure if I would clear this interview round but after 2 days, I got a mail from HR that I have cleared the round and my next round is scheduled on the coming Monday. I was relieved greatly to hear that as not clearing the first round of interview for an IIT CS graduate and employee of one of the big-named IT companies in the world, would have been a bitter pill to swallow, especially for me. I know the pressure of such expectations can be hard on oneself but sometimes one can’t escape being a mere human. I was in a more relaxed mood while facing the next round which as it turned out ended up as being my last round (will write about it in next post).

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.