Amazon Interview Question for Production Engineers


Team: kindle
Country: India
Interview Type: In-Person




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

void mirror(TreeNode root)
{
if(root==null)
return;
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
mirror(root.left);
mirror(root.right);
}

- getjar.com/todotasklist my android app December 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

T* createMirror(T* root)
{
    if(root == NULL)
    return;
    
    T* tmp = new T;
    
    tmp->val = root->val;
    tmp->left = createMirror(root->right);
    tmp->right = createMirror(root->left);
    
    return tmp;
}

- Rohit Kumra December 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You function must always return T*, so

if(root == NULL)
    return;

should be

if(root == NULL)
    return NULL;

- Selmeczy, Péter December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mirror(node1 p)
{

if(p==null)
{
return;
}
else
{
mirror(p.left);
mirror(p.right);
node1 temp=null;
temp=p.left;
p.left=p.right;
p.right=temp;

}
}

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

Node Swaptree(Struct Node* node)
{
while(node !== null)
{
Node temp1 = Swaptree(node->left);
Node temp2 = Swaptree(node->right);
node->left = temp2;
node->right = temp1;
}
return node;
}

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

"while(node !== null)" should be "if (node !== null)", otherwise it is a nice infinite loop - node is not assigned to a new value inside the loop ;)

- Selmeczy, Péter December 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is it not post order traversal and exchanging the left and right nodes

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

void mirrorImage() {
		java.util.Queue<Node> queue = new LinkedList<MirrorImage.Node>();
		queue.add(root);
		while (!queue.isEmpty()) {
			Node node = queue.poll();
			Node temp = node.right;
			node.right = node.left;
			node.left = temp;
			if (node.left != null) {
				queue.add(node.left);
			}
			if (node.right != null) {
				queue.add(node.right);
			}
		}
	}

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

<pre lang="" line="1" title="CodeMonkey53376" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}

</pre><pre title="CodeMonkey53376" input="yes">node* mirror(node *root){
mynode *temp;
if(root ==null)return null;
temp =(mynode *)malloc sizeof(node);
temp->value= root->value;
temp->lnode = mirror(root->rnode);
temp->rnode=mirror(root->lnode);
return(temp);
}
</pre>

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

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct node
{
int data;
struct node* left;
struct node* right;
};
typedef struct node Node;

Node* newNode (int data)
{
Node* temp =(Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;

return temp;
}

Node* mirrorTree (Node* node)
{
Node* temp;

if(node->left==NULL && node->right==NULL)
return node;
else{
temp = node->left;
node->left = node->right;
node->right = temp;

mirrorTree(node->left);
mirrorTree(node->right);
return (node);
}
}

void printInorder (Node* node)
{
if(node == NULL)
return;
printInorder(node->left);
printf(" %d ",node->data);
printInorder(node->right);
}

int main ()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->left->left = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);

printf("Original tree is: \n");
printInorder(root);

root = mirrorTree(root);
printf("\nThe mirror tree is:\n");
printInorder(root);

getch();
return 0;
}

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

#!/bin/bash

i=0
j=0
a=0

while read line
do
 c_count=`echo $line | awk '{print NF}'`
 for j in `seq $c_count -1 1`
  do
   array[$i,$a]=`echo $line | awk '{print $"'$j'"}'`
   echo -n "${array[$i,$a]} "
   a=`expr $a + 1`
  done
   i=`expr $i + 1`
  echo ""
done<e.txt

- Anonymous May 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def mirror_image(tree):
for i in tree:
print ','.join(str(e) for e in reversed(i))

tree=[[01], [02,03],[04,05,06,07]]
mirror_image(tree)

- Giancarlo Rubio April 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def mirror_image(tree):
for i in tree:
print ','.join(str(e) for e in reversed(i))

tree=[[01], [02,03],[04,05,06,07]]
mirror_image(tree)

- Giancarlo Rubio April 14, 2016 | 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