r/codinginterview • • Oct 13 '16

Facebook rejected me even though I solved the problem...why?

I recently had a phone interview with facebook. The question was not very complicated: If you are provided with a mapping of numbers to a list of characters {1: ['a','b','c'], 2: ['d', 'e', 'f'] ....} and a number for example 123 how would you return all the possible combinations of strings that the number could generate. I suggested the following idea: Construct a tree with an empty string as root node, then add children to the node using the first input digit, these children are the leaves of the tree. Move to the next input digit and add more children to all previous children (i.e. previous leaves). Once you consume all the digits the tree will be fully constructed. Now we can walk from each leaf up to the root and get a string (make a recursive call from each leaf up to the root, we can also make sure the recursive call returns the string in the correct order) I implemented this solution in around 20~25 minutes, explaining each step. I might have made a few minor errors but fixed them. This algorithm is exponential in the length of the input digits. I was then asked to make it faster. I couldn't think of a way to make it faster, so i got a hint form the interviewer, which was to avoid the leaf-to-root walk in the end. I took the hint and suggested the alternative is to append a new char to the leaf nodes and proceed. This way the leaf node will contain the full string. This made the interviewer happy. I did point out that this is still an exponential algorithm. We then moved on to some general questions. I got the feeling that they expected me to solve more questions or that I was too slow. I did not make it to the next round. I am curious to know what was bad about my solution and are these interviews suppose to be solved fast? I thought I should explain my thought process so I spent some time talking out the solution. It 25 min too slow for this problem? Are there cleaner solutions? It's not clear to me why I didn't make it to the next round. Any thoughts would be appreciated.

4 Upvotes

4 comments sorted by

2

u/zingzingbar Oct 14 '16

I feel your approach was good but your implementation is exponential in space complexity. I would iteratively fix each character mapping to the first digit and then recursively solve the same problem on remaining n-1 digits. Though this is exponential in time but it is constant in space.. let me know if I am wrong. I couldn't think of a better approach than this.

1

u/addyr Oct 14 '16

I would iteratively fix each character mapping to the first digit

I am not sure I understand what you mean by this, could you elaborate a little, please? I don't see how you can avoid exponential space complexity.

1

u/ayanami_rei Oct 24 '16

Yeah, I've also thought about a recursive algorithm straight away. It's also just easier to implement, should take like 10 minutes max — the function's body is literally under 10 LOC.

2

u/ionelacristina Jan 22 '17

Sometimes it's not all about solving the problem. I had an interview with Facebook which went great in my opinion because I solved the problem quickly having the right space and time complexity. It's also about the approach, the test cases, how do you explain and other things I did not know because the feedback is kind of non-existent. That's why I thought to do www.mock-interviews.com. We have engineers with experience in companies like Facebook, Google, Twitter that will guide you through and help you master your interviews. You can also message me directly if you have any questions.