Amazon Interview Question


Country: India




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

Does each "L" or "R" traversal generate exactly one "p" one in the sequence? or do these "L" and "R" 's generate sequences of different lengths?

- someone December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are these patterns O(1) in size? At the moment, I can only think of an algorithm that O(ND), where the pattern is length D.

- eugene.yarovoi December 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Some clarification : 100,70,90 imagine as a BST though the problem is not for a BST.then we have N(100) L(70) R(90).
O(n) means in almost one Tree traversal you should figure out the solution.

- Doctor.Sai December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Doctor.Sai: Your question is not clear! Can you please provide an example and describe your question.

- Jagadish December 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its still not clear how multiples sequences can be generated for a given pattern.

- Anonymous December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please be clear about the problem

- sil.abhik01 December 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The algorithm will be as follows -

Maintain 2 list -> start_node_list
and end_node_list
For each node check if the pattern exist -> put the node in the start_node and put the end node in end_node_list.

Once all the nodes are traversed you will have a list of start nodes and end nodes.
now this problem reduces to finding the longest sequence of start and end nodes.

The algorithm is O(n|p|) = O(n)

- Sachin December 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First check at every node(we can do in order traversal) if the patterns exist.
If we find a pattern at any given node, we add that in array list

Lets define a new class
pair {
Node startnode
Node endnode
int length;}

Now when you find a pattern at a node say startnode to endnode.
Now adding part
if array list already have this startnode as any of the pair's endnode, increment the lenth of that pair by 1 and replace the endnode with new endnode.
else just add this new pair in array list
once done traverse the array list to find the max length.

this will be O(n+2m) n is no of nodes and m is number of sequences
so this is O(n)

- loveCoding December 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without even understanding the question people are posting their solution...
First of all the question is not clear.. someone explain the question first then we can think of the solutions

- swathi December 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Take this as a BST (it will be easy to explain)
100,90,80,10,85,94,91,95,200,300,250.
Let p = LL
So S will be LL, LL, LL, LL, … problems simply reduces to finding longest length of only left traversal.
First get all nodes that occur as only left traversal
1. L1 = 100, 90, 80, 10.
2. L2 = 94, 91.
3. L3 = 300, 250.

Longest is L1,

- Doctor.Sai December 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

func(current,path[],pathlen)
{
if(path[]=string of pattern)
check with max, do as required.
if(curr->left!=NULL)
{path[pathlen++]=L;
func(curr->left,path,pathlen);
}
if(curr->right!=NULL)
{
path[pathlen++]=R;
func(curr->right,path,pathlen);
}
return;
}

- N December 26, 2011 | 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