Amazon Interview Question for SDE-3s


Country: United States
Interview Type: In-Person




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

void PrintTop(Node n, int cur = 0, ref int leftMost = 0, ref int rightMost = 0, bool isTop = true)
{

    if(cur == 0 && isTop)
     {
	console.writeLine(n.value);
     }
     if(c < leftMost)
     {
	console.writeLine(n.value);
	leftMost = c;
     }
     if(c > rightMost)
     {
	console.writeLine(n.value);
 	rightMost = c;
     }

     if(n.right != null)
	PrintTop(n.right, cur+1, leftMost, rightMost, false);
     if(n.left != null)
	PrintTop(n.left, cur-1, leftMost, rightMost, false);
}

- Fa1987 March 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include <iostream>
using namespace std;

int main() {
string s;
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]=='z'){
cout<<'a';
}
else{
cout<<(char)(s[i]+1);
}
}
return 0;
}

- JP March 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

constant space for data storage or for control flow as well? Is recursion considered constant space solution?

- montu March 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
- prashantkadam April 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TreeNode
{
	public TreeNode Left;
	public TreeNode Right;
	public int Value;
}

private struct ViewHelper
{
	public int Level;
	public int Value;
}

public static List<int> GetUpperView(TreeNode root)
{
	var list = new List<int>();
	
	if (root == null)
		return list;
	
	int leftCount = 0;
	int rightCount = 0;
	
	GetLimits(root, 0, ref leftCount, ref rightCount);
	
	leftCount = Math.Abs(leftCount);
	int n = leftCount + rightCount + 1; 
	
	var a = new ViewHelper[n];
	a[leftCount].Value = root.Value;
	GetUpperView(root, 0, 0, leftCount, a);
	
	foreach (var item in a)
		list.Add(item.Value);
	
	return list;
}

// Iterates one on the tree to get the limits, left and right count
private static void GetLimits(TreeNode node, int index, ref int leftCount, ref int rightCount)
{
	if (node == null)
		return;
	
	leftCount = Math.Min(leftCount, index);
	rightCount = Math.Max(rightCount, index);
	
	GetLimits(node.Left, index - 1, ref leftCount, ref rightCount);
	GetLimits(node.Right, index + 1, ref leftCount, ref rightCount);
}

private static void GetUpperView(TreeNode node, int row, int level, int leftCount, ViewHelper[] a)
{
	if (node == null)
		return;
	
	int index = leftCount + row;
	if (a[index].Level < level)
	{
		a[index].Level = level;
		a[index].Value = node.Value;
	}
	
	GetUpperView(node.Left, row - 1, level + 1, leftCount, a);
	GetUpperView(node.Right, row + 1, level + 1, leftCount, a);
}

- hnatsu April 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void top_view(Node root)
{
	printleft(root);
    	printright(root.right);
}

void printleft(Node root){
    	if (root == null){
        	return;
   	}
    	printleft(root.left);
       System.out.print(root.data+" ");
}

void printright(Node root){
    	if(root == null){
        	return;
    	}
    	System.out.print(root.data+" ");
    	printright(root.right);
}

- guruz09 May 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printTopView(Node root) {

        if (root == null) return;
        Queue<Node> q = new LinkedBlockingQueue<>();
        root.weight = 0;
        q.add(root);
        Map<Integer, Node> nodeMap = new TreeMap<>();

        while (!q.isEmpty()) {
            Node node = q.poll();
            int weight = node.weight;
            //insert weight wise nodes so the latest from below weight will replace the node on vertical
            // above weight
            if (!nodeMap.containsKey(weight)) {
                nodeMap.put(weight, node);
            }

            if (node.left != null) {
                node.left.weight = weight - 1;
                q.add(node.left);
            }
            if (node.right != null) {
                node.right.weight = weight + 1;
                q.add(node.right);
            }
        }

        nodeMap.forEach((k, v) -> {
            System.out.println("" + v.data + " , ");
        });
    }

- Sanjay Nayak June 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <iostream>
using namespace std;

int main() {
string s;
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]=='z'){
cout<<'a';
}
else{
cout<<(char)(s[i]+1);
}
}
return 0;
}

- JP March 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

struct Node {
	int val;
	Node *left;
	Node *right;
};

vector<int> printTopView(Node *root) {
	if (!root) return vector<int>();

	vector<int> result;
	Node *node = root;
	while (!node) {
		result.push_back(node->value);
		node = node->left;
	}
	result = reverse(result);
	node = root->right;

	while(!node) {
		result.push_back(node->value);
		node = node->right;
	}

	return result;
}

- Abhishek March 15, 2017 | 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