Directi Interview Question for Software Engineer / Developers


Country: India




Comment hidden because of low score. Click to expand.
1
of 5 vote

Let us define the notion of 'root' and 'parent' on the tree. Let the root of the tree be 'r'.

Suppose that for every node 'x', we have computed the total weight of the subtree rooted at 'x' (this includes the weight of 'x'). Let us call this sum: S(x).

Then for every edge (u,v) in the tree, where 'u' is the parent of 'v', the sums of the 2 trees formed on removing edge (u,v) are S(v) and S(r) - S(v). In this way, we can iterate over all the edges and return the minimum possible difference.

Time complexity: O(N), space O(N).

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

read the question properly. how can you implement the tree there can be many child to a parent and you are given the index of the array so by that how will you add the node to the tree?

- sameer July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't understand your question. Implementing a tree given an edge list should not be difficult.

- anujkaliaiitd July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution seems to be right but everytime calling the function S() on child node of an edge will not have a time complexity of O(1) making the total time complexity higher.

- Arnesh July 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It won't be called every time. The calculation will be done once for the entire tree and the sum values stored in a new tree structured analogously to the original input.

- eugene.yarovoi July 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so how would u define s(3) be 14 i.e. it does not include 1 as its child..

- Anonymous August 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What ever you suppose, they is the only problem..!!!
you write the algorithm for that only, not for that we after that, after that can be done by anyone who know something about programming. give us algorithm in support of your suppose, i have posted O(N^2) solution. as according to problem specification (10 sec, of time), it require N^2 time.

- sonesh November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sonesh: The problem does not require O(n^2) time. This solution is correct.

Looking at the constraints, O(n^2) would potentially time out, as there are up to 100 test cases. You have to read 100 (number of test cases) * 1000 (size of each test case) lines from input...that's why they didn't go for bigger inputs here.

It seems that you have some questions about how this works, but I can't understand what specifically you want to know. Can you formulate your question more clearly?

- eugene.yarovoi November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We can do it like this..!!!

#include <iostream>
#include <cmath>
using namespace std;

int sum;

struct Node
{
	int id;
	Node *next;
};

struct node
{
	int id;
	int pid;
	node *next;
};

class Queue
{
	private:
		node *head;
		node *tail;
	public:
		Queue()
		{
			head = NULL;
			tail = NULL;
		}
		void insert(int id,int pid)
		{
			node *temp = new node();
			temp->id = id;
			temp->pid = pid;
			temp->next = NULL;
			if(tail)
				tail->next = temp;
			tail = temp;
			if(!head)
				head = temp;
		}
		node* remove()
		{
			node *temp = new node();
			temp = head;
			head = head->next;
			return temp;
		}
		void display()
		{
			node *temp = head;
			if(temp)
			{
				cout<<"("<<temp->id<<","<<temp->pid<<") -> ";
				temp = temp->next;
			}
		}
		bool empty()
		{
			if(head)
				return false;
			else
				return true;
		}
};

class List
{
	private:
		Node *head;
		int weight;
		int id;
	public:
		List()
		{
			head = NULL;
			weight = 0;
			id = 0;
		}
		void setpar(int d,int w)
		{
			id = d;
			weight = w;
		}
		void insert(int a)
		{
			Node *temp = new Node();
			temp->id = a;
			temp->next = head;
			head = temp;
		}
		void cal_sum(int a, Queue *queue)
		{
			sum = sum + weight;
			if(head)
			{
				Node *temp = head;
				while(temp)
				{
					if(temp->id != a)
						queue->insert(temp->id,id);
					temp = temp->next;
				}
			}
		}
};

int main()
{
	Queue queue;
	node *temp = new node();
	int T,N,i,diff,sum1,sum2,w;
	cin>>T;
	while(T--)
	{
		cin>>N;
		int A[N-1],B[N-1];
		List list[N];
		for(i=0;i<N;i++)
		{
			cin>>w;
			list[i].setpar(i,w);
		}
		for(i=0;i<N-1;i++)
		{
			cin>>A[i]>>B[i];
			list[A[i]].insert(B[i]);
			list[B[i]].insert(A[i]);
		}
		diff = -1;
		for(i=0;i<N-1;i++)
		{
			sum = 0;
			queue.insert(A[i],B[i]);
			while(!queue.empty())
			{
				temp = queue.remove();
				list[temp->id].cal_sum(temp->pid,&queue);
			}
			sum1 = sum;
			sum = 0;
			queue.insert(B[i],A[i]);
			while(!queue.empty())
			{
				temp = queue.remove();
				list[temp->id].cal_sum(temp->pid,&queue);
			}
			sum2 = sum;
			if(diff == -1)
				diff = abs(sum1-sum2);
			else if(diff > abs(sum1-sum2))
				diff = abs(sum1-sum2);
		}
		cout<<diff<<endl;
	}

}

here n = 1000 and t = 100, my algorithm takes n^2 time, so here the total it become 10^3*10^3*10^2 = 10^8, can be done in 1 sec. by 10^9 calculation/sec. machine.

- sonesh November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This seems like TWO good but unrelated questions. Or, I am I overtired?

- ChrisRussell41 July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
5
of 5 votes

Hi, Eugene. Thanks for the kick in the head. I see that it's a single problem now :-) In the 1st test case, removal of the edge 2,1 yields two subtrees of weight 15 and 8 respectively w/difference = 7. In the 2nd test case, removal of edge 3,1 yields subtrees of weight 14 and 27 w/difference = 13. So, now I at least understand the problem and what's being asked. Off to code it up. Thanks again - C

- ChrisRussell41 July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

it is a BFS question
save the tree as a graph in 2-d matrix or adjacency list form.
calulatemindiff()
{
for each edge
{
apply bfs on left part and then on right par to calculate the sum of its both side tree

calculate the difference of these to sum.

update the global diff accordingly.
}
return globaldiff;
}

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

this might be a good solution....:)

- Ankit July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This soution is O(m*(m+n)) because you are doing m BFSes. Read my comment above for an O(m+n) solution.

- anujkaliaiitd July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You're basically iterating over all nodes for each edge. That's quadratic complexity instead of the linear complexity achieved by another solution here.

- eugene.yarovoi July 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The another solution doesn't make sense as I see it. First of all how'd you construct a tree where the number of children are not specified? There are methods definitely but then they would take more time not linear. Correct me if I'm wrong

- teja.1305 August 07, 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