Akamai Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Approach:
=========
We can implement it using BFS search ...
1. Use a queue and add root node inside it.
2. Now iterate over queue untill it not empty

Algorithm:
==========
Queue level1 = new Queue();
level1.add(rootNode);
Node lastNode = null;
while(!level1.isEmpty()){
	Node currentParent = queue.dequeue();
	Queue level2 = new Queue()
	if(currentParent.hasLeftNode()){
		Node leftChild = currentParent.leftNode();
		if(lastNode != null){
			lastNode.next() =  leftChild;
		}
		lastNode = leftChild;
		level2.add(leftChild);
	}
	if(currentParent.hasRightNode()){
		Node rightChild = currentParent.rightNode();
		if(lastNode != null){
			lastNode.next() =  rightChild;
		}
		lastNode = rightChild;
		level2.add(rightChild);
	}

}

- Ajeet July 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how does this work? you are not adding anything to queue1 later in while loop, and in every loop you create a new queue2 ? i doubt this code works

- talktomenow July 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yea, its my BAD i forgot to assign it back to level1, thanks for the correction:

Algorithm:
==========
Queue level1 = new Queue();
level1.add(rootNode);
Node lastNode = null;
while(!level1.isEmpty()){
	Queue level2 = new Queue();
	while(!level1.isEmpty()){
		Node currentParent = queue.dequeue();
		if(currentParent.hasLeftNode()){
			Node leftChild = currentParent.leftNode();
			if(lastNode != null){
				lastNode.next() =  leftChild;
			}
			lastNode = leftChild;
			level2.add(leftChild);
		}
		if(currentParent.hasRightNode()){
			Node rightChild = currentParent.rightNode();
			if(lastNode != null){
				lastNode.next() =  rightChild;
			}
			lastNode = rightChild;
			level2.add(rightChild);
		}
	}
	level1 = level2;
}

Space O(L) where L = number of nodes at one level, Time O(N)

- Ajeet July 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is the next pointer supposed to point to the node on the same level? If yes what happens if there are no nodes on the right at a give level.

- kr.neerav July 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I updated the question. if no node on right or if the node itself if right most, the next points to null

- talktomenow July 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have attempted a possible solution, but may still needs to be refined/corrected

change_tree(struct node *root)
{

if (root == NULL)
   return;
root->next = NULL;
if (root->left) 
  root->left->next = root->right;
if (root->right)
  root->right->next = NULL;

if (root->left){
  assign_next_from_parent(root->left->left,root->left);
  assign_next_from_parent(root->left->right,root->left);
}
if (root->right) {
  assign_next_from_parent(root->right->left,root->left);
  assign_next_from_parent(root->right->right,root->left);
}

assign_next_from_parent(struct node *node, struct node *parent)
{
  if(parent->right)
    node->next = parent->right;
  elseif (parent->next) {
   if (parent->next->left)node->next = parent->next->left;
   else   node->next = parent->next->right;

  } else {
     node->next = NULL;
 }

assign_next_from_parent(node->left,node);
assign_next_from_parent(node->right, node);

}

- talktomenow July 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have a solution O(1) space and O(n) time, where n - number of nodes.

void connect(TreeLinkNode *root) {
        if(!root) return;
        TreeLinkNode * cur = root;
        root -> next = NULL;
        while( cur -> left ){
            
            TreeLinkNode * connect = cur;
            while (connect -> next){
                connect -> left -> next = connect -> right;
                connect -> right -> next = connect -> next -> left;
                connect = connect -> next;
            }
            cur = cur -> left;
            connect -> left -> next = connect -> right;
            connect -> right -> next = NULL;
            
        }

- Dima Soroka July 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL

- Dima Soroka July 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct TreeNode {
	int data;
	TreeNode* left;
	TreeNode* right;
	TreeNode* thePtr;

	TreeNode(int data) : data(data), left(NULL), right(NULL) {}
	TreeNode(int data, TreeNode* left, TreeNode* right) : data(data), left(left), right(right) {}
};


void addLinks(TreeNode* root) {
	if (!root) return;
	queue<TreeNode*> nq;
	nq.push(root);
	nq.push(NULL);
	TreeNode* prev = NULL;

	while (!nq.empty()) {
		TreeNode *t = nq.front();
		nq.pop();
		if (prev)
			prev->thePtr = t;
		prev = t;
		if (t && t->left)
			nq.push(t->left);
		if (t && t->right)
			nq.push(t->right);
		if (!t && !nq.empty())
			nq.push(t);
	}
}

TreeNode* makeNode(int data, TreeNode *left = NULL, TreeNode *right = NULL) {
	return new TreeNode(data, left, right);
}


void printLinks(TreeNode *root) {
	TreeNode* t = root;
	while (t) {
		cout << t->data << ": ";
		TreeNode* tptr = t;
		while (tptr) {
			cout << tptr->data << " ";
			tptr = tptr->thePtr;
		}
		cout << endl;
		t = t->left;
	}
}

void test_addLinks() {
	TreeNode *root = makeNode(10);
	root->left = makeNode(12);
	root->right = makeNode(15);

	root->left->left = makeNode(8, makeNode(9), makeNode(4));
	root->left->right = makeNode(14, NULL, makeNode(7));

	root->right->left = makeNode(45, makeNode(2), makeNode(11));
	root->right->right = makeNode(26, NULL, makeNode(12));


	addLinks(root);
	printLinks(root);
}


int main() {
	test_addLinks();
	cin.ignore();
}

- Hari Prasad Perabattula July 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Find the height of the tree.
2) Use loop from 1 to height of tree for level traversal of the tree.
3) Add all nodes of the same level to a queue and link them.

- Amit Kumar July 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Find the height of the tree.
2) Use loop from 1 to height of tree for level traversal of the tree.
3) Add all nodes of the same level to a queue and link them.

- Amit Kumar July 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No assumption seems to be made about the the data structure of the binary tree. If its implemented as an array, then finding nodes to the right boils down to a matter of just examining the index.

#include <vector>

struct node
{
	unsigned data;
	bool is_null;
	node *right;
}

void connect_right(vector<node> tree)
{
	for(int i = 0; i < tree.size; i++)
	{
		if(!tree[i].is_null)
		{
			for(int j = i + 1; (j & (j + 1)) == 0; j++)
			{
				if(!tree[j].is_null)
				{
					tree[i].right = tree + j;
				}
			}
		}
	
	}
}

- Johnnie July 17, 2014 | Flag Reply


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