Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

#include<iostream>
#include<queue>

using namespace std;

class node{
public:
  int data;
  node *left;
  node *right;
  node(int d);
  bool isLeaf();
};

node::node(int d){
  data = d;
  left = NULL;
  right = NULL;
}

bool node::isLeaf(){
  return ((this->right == NULL) && (this->left == NULL));
}

void programFailure(){
  cout<<"Invalid pyramid"<<endl;
  exit(1);
}

bool isInvalid(node *n){
  if((n->left == NULL) ^ (n->right == NULL)){
    return true;
  }
  return false;
}

typedef priority_queue<int> mpq;

void findMaxSum(node* n, int sum, mpq &q){
  if(n == NULL){
    programFailure();
  }
  if(isInvalid(n)){
    programFailure();
  }
  sum += n->data;
  if(n->isLeaf()){
    q.push(sum);
    sum -= n->data;
    return;
  }

  findMaxSum(n->left, sum, q);
  findMaxSum(n->right, sum, q);
  sum -= n->data;
}

int main(){
  node *n = new node(55);
  n->left = new node(94);
  n->right = new node(48);
  n->left->left = new node(95);
  n->left->right = new node(30);
  n->right->left = n->left->right; // 48 -> 30
  n->left->left->left = new node(77);
  n->left->left->right = new node(71);
  n->left->right->left = n->left->left->right; // 30 -> 71
  n->left->right->right = new node(26);
  n->right->right = new node(96);
  n->right->right->left = n->left->right->right;
  n->right->right->right = new node(67);

  mpq q;
  int sum = 0;
  findMaxSum(n, sum, q);
  cout<<q.top()<<endl;
  return 0;
}

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

Can you please explain the reason for down vote? That might help me improve.

- Anonymous March 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very nice solution!
3 comments:
1. The function should return the value and not expect main to collect it from the queue (exposing the implementation is generally a bad idea)
2. decreasing sum doesn't have any effect because there is no use of sum afterwards
3. you don't really need the queue to solve this (which I guess is why you got the down vote). It adds space complexity and time complexity for the insertions. It would make sense if the question asked for all the sums sorted but all you need here is the maximal value. Just recursively select the son with the maximal sum and return that value plus the root's value.

- elkon March 13, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findMaxPath(a):
    maxTotal = 0
    i = 0
    j = 0
    for row in a:
        midval = a[i][j]

        leftval = rightval = 0
        if j > 1:
            leftval = a[i][j-1]

        if i != 0:
            rightval = a[i][j+1]

        if midval >= leftval and midval >= rightval:
            max = midval
            if i > 0:
                print("remained at the same column")
            else:
                print("began journey")
        elif leftval >= rightval:
            max = leftval
            j -= 1
            print("walk one step left")
        else:
            max = rightval
            j += 1
            print("walk one step right")

        maxTotal += max
        print(max)
        i += 1
        print("walk down")

    print(maxTotal)

a = [[55], [94,48], [95, 30, 96], [77, 71, 26, 67]]
findMaxPath(a)

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

static class Result {
int value;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int [][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j=0; j <= i; j++)
a[i][j] = in.nextInt();
}
int k = findMaxSum(a);
System.out.println(k);
}
private static int findMaxSum(int[][] a) {
int n = a.length;
int sum = 0;
Result result = new Result();
findMaxSumHelper(a, 0, 0, sum, result);
return result.value;
}

private static void findMaxSumHelper(int[][] a, int i, int j, int sum, Result result) {
if (i >= a.length) {
if (result.value < sum)
result.value = sum;
return;
}
int origSum = sum + a[i][j];
findMaxSumHelper(a, i+1, j, origSum, result);
findMaxSumHelper(a, i+1, j+1, origSum, result);
}

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

static class Result {
		int value;
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int [][] a = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j=0; j <= i; j++)
			a[i][j] = in.nextInt();
		}
		int k = findMaxSum(a);
		System.out.println(k);
	}	
	private static int findMaxSum(int[][] a) {
		int n = a.length;
		int sum = 0;
		Result result = new Result();
		findMaxSumHelper(a, 0, 0, sum, result);
		return result.value;
	}
	
	private static void findMaxSumHelper(int[][] a, int i, int j, int sum, Result result) {
		if (i >= a.length) {
			if (result.value < sum)
				result.value = sum;
			return;
		}
		int origSum = sum + a[i][j];
		findMaxSumHelper(a, i+1, j, origSum, result);
		findMaxSumHelper(a, i+1, j+1, origSum, result);
	}

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

It's basically finding max sum path in binary tree.

- sunitha.prabhu April 26, 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