Pergunta de entrevista da empresa Meta

Print a binary search tree. Each level on a new line.

Respostas da entrevista

Sigiloso

15 de mai. de 2011

deveffort: i believe you need to enqueue sentinel only if queue is not empty. otherwise you will infinitely queue/dequeue last sentinel

1

Sigiloso

16 de fev. de 2011

A very simple solution is to scan the tree in BFS, enqueue nodes and enqueue a sentinel after the right most child. 1. Enqueue root. Insert sentinel 2. Dequeue while queue not empty If node Enqueue left child and right child Print node if sentinel Print new line Enqueue sentinel

2

Sigiloso

5 de dez. de 2010

I thought the problem itself was simple, i discussed the question with him for a min and then jumped into writing the code. The code itself was simple, all we need is a breadth First traversal and store nodes in a queue to keep track of all nodes and print them. The only place it took me little time was how to print a new line after every line, which i solved by keeping track of number of nodes i have printed. I coded the complete solution and was very confident. He seemed to be happy, but the feedback i received from the recruiter was that the interviewer wasn't happy with my coding skills. May be he wanted some really optimized alog...but not sure how much better can you do than O(n) since one has to visit all the nodes.

1

Sigiloso

9 de dez. de 2010

It's important that you churn out 'near perfect' code at the first hack. FB is asking average difficulty questions which could be done by average coder. But the trick is FB only choosing those could write 'near perfect', 'naturally optimized' code at the first attempt.