Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

public static class Node {
		
		public int memorySize;
		public Node leftChild = null;
		public Node rightChild = null;
		
		public Node(int memorySize) {
			this.memorySize = memorySize;
		}
	}
	
	public static int getBestFit(Node root, int target) {
		return getBestFit(root, target, -1);
	} 
	
	public static int getBestFit(Node root, int target, int bestFit) {
		if(root == null) {
			return bestFit;
		} else {
			if(root.memorySize >= target && ( bestFit == -1 || root.memorySize < bestFit)) {
				bestFit = root.memorySize;
			}
			if(root.memorySize < target) {
				return getBestFit(root.rightChild, target, bestFit);
			} else if(root.memorySize > target) {
				return getBestFit(root.leftChild, target, bestFit);
			}
			return bestFit;
		}
	}

- Breathes_In_Binary June 06, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is this not more like given a BST and one integer k, finding sum of the nodes which can make to that k ?

- gopi.komanduri June 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

using namespace std;
struct node
{
    struct node*left;
    struct node*right;
    int key;
    static int maxheight;
};

int node::maxheight =0;

struct node * insert(struct node*root,int key,int height=0)
{
    if(root == NULL)
    {
        if(height>node::maxheight) node::maxheight=height;
        struct node * temp=new node;
        temp->key = key;
        temp->left=temp->right=NULL;
        return temp;
    }
    if(key > root->key)
    {
        root->right = insert(root->right,key,height+1);
    }
    else if(key < root->key)
    {
        root->left = insert(root->left,key,height+1);
    }
}

void printTree(struct node*root, int level=0, int maxheight=node::maxheight)
{
    if(root==NULL) return;
    printTree(root->left,level+1);
    for(int i=maxheight-level;i>=0;i--) cout<<"  ";
    cout<<root->key<<endl;
    if(root->key<10)  cout<<" "<<endl; 
    printTree(root->right,level+1);
    /*for(int i=maxheight-level;i>=0;i--) cout<<"  ";
    cout<<root->key<<endl;
    if(root->key<10)  cout<<" "<<endl; */
}

struct node * best_fit_memory(struct node* root,int input_block_size,struct node* best_fit=0)
{
    if(root)
    {
        if(root->key > input_block_size && ((best_fit && best_fit->key > root->key)||!best_fit))
        best_fit = root;
    }
    else
    {
        return best_fit;
    }
    if(input_block_size > root->key)
    {
        best_fit = best_fit_memory(root->right, input_block_size,best_fit);
    }
    else if(input_block_size < root->key)
    {
        best_fit = best_fit_memory(root->left, input_block_size,best_fit);
    }
    else
    {
        best_fit = root;
        //return root;
    }
    return best_fit;
}

int main()
{
    struct node*root=NULL;
    root = insert(root,8);
    printTree(root);
    cout<<"------------------------------------------------------------------------------"<<endl;
    root = insert(root,7);
    printTree(root);
    cout<<"------------------------------------------------------------------------------"<<endl;
    root = insert(root,10);
    printTree(root);
    cout<<"------------------------------------------------------------------------------"<<endl;
    root = insert(root,9);
    printTree(root);
    cout<<"------------------------------------------------------------------------------"<<endl;
    root = insert(root,5);
    printTree(root);
    cout<<"------------------------------------------------------------------------------"<<endl;
    struct node* result_best_fit = best_fit_memory(root,6);
    cout<<"for input "<<6<<"resultbest fit = "<<result_best_fit->key<<endl;
}

- mohapatrasandeep60 June 12, 2018 | 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