Amazon Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

For those who(including myself) don't know what is N-ary Tree, its a simple tree where each node can have N child nodes.
Data Structure I use is

Class Node
{
	int value;
	bool isVisited;
	ArrayList<Node> childs;
}

Function to find if it has loop or not, I think it should work i havent tested it though.

boolean isLoop(Node root)
{
	root.isVisited=true;
	bool isloop=false;
	for(int i=0;i<root.Childs.Count();i++)
	{
	if(root.Childs[i].isVisited==true)
	             return true;
	else
                   isloop=isLoop(root.childs[i]);
                    if(isloop==true) return true;
	}
	
return isloop;

}

- billu March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

DFS is good. just elaborate if it's a graph. we probably don't have a root.
...O...O
../..\../..\
O...O...O
in the above graph, we'll need to check every node for its children. and you can meet the same node more than once like in the example and it may not be a cycle. so only visited flag is not enough.
we can add one more flag for detecting cycles in the recursion phase.
so just add the stack flag in the code.
boolean isLoop(Node root,HashMap hash)
{if(!root.isVisited){
root.isVisited=true;
bool isloop=false;
hash.put(root,true);
for(int i=0;i<root.Childs.Count();i++)
{
if(hash.get(root.Childs[i])==true)
return true;
else if( !root.childs[i].isVisited&&isLoop(root.childs[i],hash))
return true;
}
hash.put(root,false);}
return false;
}

int main(){
HashMap hash=new ....;
for(Node x:all nodes){
if(isLoop(x,hash)) return true;}
return false;}

- zyfo2 March 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I would say its best to use data structure like this

struct t {
int n;
struct t *kids;
struct t *siblings;
}

Using above structure, while constructing the tree assume kids are always on left of a node and siblings on the right side of a node. This would look like a binary tree.

Hence at the end you can use DFS to check whether cycle is there or not.

Correct me if I am wrong.

- Ashish19121 March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

DFS or simply, say that we would traverse the tree marking each node as seen. If while traversal we ever find a node which is already visited then the tree has a cycle.

- Second Attempt March 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

struct NTreeNode
{
	vector<NTreeNode<T>*>*  chlildren; // max length of vector = N
	T data;
};

- Gupt March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

cycles can be found by using a hash table, and marking a node as soon as you visit it.

- Gupt March 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Gupt Except binary tree, to traverse any other tree we need to visit more than once. So it seems it may not possible to say there is a loop/cycle using that logic.

- Dinesh March 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

template<class T>
 void NTree<T>::Preorder(NTreeNode<T>* node)
{
    if (!node) return;

    cout << node->m_data;

    vector<NTreeNode<T>*>::iterator iter;

    iter = node->m_listChildren.begin();
    for (; it != node->m_listChildren.end() ;it++) 
    {
        Preorder(*it);
    }
}

@Dinesh, I dont think so, a node will be visited only once. Anything to support your argument?

- Gupt March 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

class node
{
int vals[N-1];
node children[N];
node parent;
}
Add one extra member for node structure "Node *Parent"
Write a function which takes node and it's parent as args.
Find_Loop(Node *root, Node *Par) //par=NULL for root node
{
if ( root->parent != par )
{
print "Loop found";
exit;
}
//visit the node
for(int i=0;i<N && root[i+1]!=NULL ;i++)
Find_Loop( root->children[i], root);
}

For every recursive call we will pass child node and current node(as parent) as args.

Please correct me if anything is wrong.

- Dinesh March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include<conio.h>
#include<stdio.h>
void MergesortedArray(int a[],int b[],int n,int m)
{
    int i=n-1,j=m-1,k=m+n,temp;
//int ar1[4]={5,15,30,50};    int ar2[7]={25,35,45};
    while(i != -1 && j!=-1)
    {
            if(b[j]>a[i])
            {
                b[(--k)]=b[j];
                j--;
            }
            else
            {
              b[(--k)]=a[i];
              i--;
            }
    }
    if(n>m)
    {
        while(i!=-1)
        {
              b[--k]=a[i];
              i--;
        }
    }
    for(i=0;i<7;i++)
    {
        printf("%d ",b[i]);
    }
}
int main()
{
    int ar1[4]={5,15,30,50};
    int ar2[7]={25,35,45};
    MergesortedArray(ar1,ar2,4,3);
}

- Complicated Sam March 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sam misplaced answer

- Gupt March 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about using an adjacency matrix where Mat[i][j] = 1 if there is an edge from i to j, and checking to see the values of Mat[i][j] == Mat[j][i] for all i,j above the diagonal.

- squared March 12, 2013 | 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