Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

A very easy recursive solution without building the tree
#include<stdio.h>
#define max(x,y) x>y?x:y
int findlen(char a[],int *x)
{
if(a[*x]=='\0')
return 0;
(*x)++;
if(a[*x-1]=='L')
return 1;
int y=findlen(a,x),z=findlen(a,x);
return (1+(max(y,z)));
}
int main()
{
char a[]="PPPLLLPLL";
int x=0;
printf("depth of given tree=%d\n",findlen(a,&x));
return 0;
}

- Amit June 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good solution. +1. Your tree depth is off by 1 though. The root node is considered at depth 0.

Couple suggestions:
1. Try to put explanation next time. This will help us understand your solution better.
2. Use triple {-} when posting code.

- oOZz June 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain your algorithm? Curious about how recursion works here, provided that only string.

- Kallu June 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain your approach?

- prity June 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will this if condition if(a[*x-1]=='L') doesn't throw exception when x=0 ?

- Anonymous June 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous--no,i incremented *x by 1 before checking a[*x-1]...the algorithm is simple enough
1.make a node
2.call for left subtree
3.call fro right subtree(hence 2 recursive calls to findmin)
4.return max of height of left and right subtree +1

- Amit July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution...height is off by 1...just return 0 for (a[*x-1]=='L') as height of a leaf node is 0;

- Anonymous July 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Following is the code in python.

def preorder_depth(my_str, cur_node_index, parent_depth, max_depth_sofar):
    if my_str[cur_node_index] == 'L':
        if max_depth_sofar < ( parent_depth + 1 ):
            max_depth_sofar = parent_depth + 1
        return (cur_node_index+1, max_depth_sofar)
    else:
        # left child
        (next_node_index, max_depth_sofar) = \
                   preorder_depth(my_str, cur_node_index+1, parent_depth+1, max_depth_sofar)
        # right child
        (next_node_index, max_depth_sofar) = \
                   preorder_depth(my_str, next_node_index, parent_depth+1, max_depth_sofar)

        return (next_node_index, max_depth_sofar)


## MAIN ##
print preorder_depth("PPPLLLL", 0, 0, 0)
print preorder_depth("PPPLPLLLL", 0, 0, 0)

- whatevva' June 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The logic here is that in the given preorder traversal string, LL divides tree into two sub strees (left and right). The string which is left side of LL is represents left sub tree and right rest of the tree is right sub tree.

So, the depth of the tree is now Max(num of Ps in LST, 1 + num of Ps in RST) (+ 1 should be added to RST). Example:

P
P P
L L L P
L L

Preorder traversal string: PPLLPLPLL. Depth/height of LST is 2 and RST is 3. Finally, height of tree is 3.

- Anonymous June 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Some special cases:
1. LST has only one (and other sub tree is big). In this case, first L represents end of LST.
2. RST has only one (and other sub tree is big). In this case, first L represents end of RST.
3. Tree has only one node or three nodes.

- Kallu June 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DepthFinder {

	private int counter = 0;
	public int findDepth(char[] arr, int active, int depth) {
		if(counter >= arr.length)
			return depth;
		
		
		int i = counter++;
		if(arr[i] == 'P'){
			int dl = findDepth(arr, i, depth + 1);
			int dr = findDepth(arr, i, depth + 1);
			return (dl>dr)?dl:dr;
		}else{
			return depth;
		}
	}
}

- Arpit June 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Case; pplplpl
1) p
P p
L l p
L
2) p
P
L p
L p
L

Hr d depth differs. hence it cant b found. Corrrect if am wrong

- Anonymous June 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Case; pplplpl
1) p
P p
L l p
L
2) p
P
L p
L p
L

Hr d depth differs. hence it cant b found. Corrrect if am wrong

- Anonymous June 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, would you please clarify is this tree balanced? With currently having requirements it's ambiguous how to rebuild a tree, consider the following string: "ppppllppll". There are 2 trees, that meet given requirements.

- Marcello Ghali June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am doubtful whether we can construct an unique tree by just having the preorder sequence. We will need inorder sequence here as well to construct a unique tree and hence the depth cannot be determined.

- Kiran June 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple solution, since each node can be complete or has to be a leaf
1.If value is P, add one to height and move ahead by one on string
2.If value is L and next value is P move ahead on string and dont add to height
3.If value is L and next value is L, move ahead on string and reduce height by 1
4.String will always end on LL, when this happens dont subtract 1 as we are not moving up
output the maximum value height attained during scan

- praveen August 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int depth(string s,int& index)
{
if(index >= s.size()) {
return 0;
}
int rsl(0);
if(s[index] == 'P') {
int ld = depth(s,index+1);
int rd = depth(s,index+1);
rsl = 1 + max(ld,rd);
}
return rsl;
}

- Anonymous June 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain how does this works?. How do you know about left and right trees provided only string is given.

- Kallu June 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Isn't it possible to have multiple trees with same preorder traversal?

For example consider PPPPLLP

Both the below trees will have the above traversal, and they have different depths

P
    P      P
P     P
     L     L

Depth is 3

           P 
        P    P
     P
  P
L  L

Depth is 4

- IntwPrep July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hmm got my whitespaces screwed up. One more shot.

The tree with depth 3

P
    P    P
P     P
     L     L

- IntwPrep July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I over-looked that each node can only have 0 or 2 children. Please ignore my post.

PS: Is there a way to delete/edit posts?

- IntwPrep July 18, 2013 | 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