Interview Question


Team: SQL Server / DPG Group
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

use Breadth-first search.

- BeautifulCode October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assuming every node contains 26 pointers, a node can be represented using 4 bytes (the first 6 representing the node information and the next 26 indicating the presence of child pointers)[1]

Traverse pre-order. Write '0' for an internal node and '1' for a leaf node. Write the 4 bytes representing the node after that. Do a breadth first traversal on the tree and keep writing these 4 bytes one after the other. You will end up taking 4n bytes to represent the tree.

While deserializing, create blocks of 4 bytes each and put it in a queue. Dequeue to get the first node. Make it the root. Then dequeue k blocks where k is the number of children of the root, attach them appropriately. Move to the first child node which has non-zero children. Repeat process.

[1] Assumption for ease of implementation. There may be other ways.

- dr.house October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think we will need 4 bytes.

en.wikipedia.org/wiki/Binary_tree#Encodings

- rix October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We are talking about "tries", rix. They are not binary trees

- dr.house October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I can see a simple extension to the algorithm mentioned in the wiki to satisfy a general k-array tree without using a bit more or less.

I actually misread your post.
You have the same argument as mentioned in the wiki, except that you encode data along with the child nodes presence (0 or 1). Whereas the wiki keeps them separate.
I'm not sure about your assumption "using 6 bits for storing node information...".

So yep, all is well.

- rix October 16, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More