Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

You have to take into account the fact that cuboids might not fit directly into the bigger cuboid- the volumes might be divisible, but physically, it might not be viable.

For example, if we take rectangles of size 4x3, our area would be 12. If we try to fit squares of 2x2 into that area, at first we might think just to divide 12 by 4 to get 3 squares that fit, but if you actually draw it out, you can only really get 2 squares to fit.

So, we need to break this down based on how many ways we can divide each dimension by 2, and take the floor of that, since anything above that would just be extra space.

boolean canFit(int x, int a, int b, int c) {
	
		return (((a/2)* (b /2) * (c/2)) >= x);
	}

- SK July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

X was not given. We need to find x too. Its like volume of.chocolates will.be 2 cm3 and one is free to change thier dimensions to fit in the given box.

- arpit.gautam July 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool can_fit(int x, int a, int b, int c) {
  #define re(x) (x-(x%2)) /* round down to even */
  int n_choc = (re(a)*re(b)*re(c))/8;
  return x <= n_choc;
}

- pb July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question says 2 cm3 so the sides are of length cubed root of 2
It is not 8 cm3 which would be a cube 2x2x2

- bealebacchus July 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't this a simple math problem? Compute total volume of box - as a*b*c. Then find out how many chocolates can fit in to the box by division.

VolBox = a*b*c
VolChoc = 2

X = VolBox / VolChoc

If X >= 1 - return X and say atlteast X chocolates can fit into the box.

else. return 0 and say no chocolates of 2cm3 can fit into the box.

Unless the question is incomplete, this should be the solution

- J July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fit(int x, int a, int b,int c){

int number_of_box = (a*b*c)/2;

return floor(number_of_box );

- Anonymous July 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Because they're cuboids, the chocolates can be any shape. Max volume of chocolate equals volume of box. Max count of chocolates equals half the volume of chocolate (in cm^3) or half the volume of the box. Am I missing something?

static int howManyFit(float length, float width, float height) {
        return (int)(length * width * height) / 2;
    }
    
    static boolean canFit(int count, float length, float width, float height) {
        return count <= howManyFit(length, width, height);
    }
    
    public void testHowManyFit() {
        assertEquals(1, howManyFit(1,1,2));
        assertEquals(true, canFit(1, 1,1,2));
        assertEquals(false, canFit(2, 1,1,2));
        assertEquals(2, howManyFit(1,2,2));
        assertEquals(true, canFit(2, 1,2,2));
        assertEquals(false, canFit(3, 1,2,2));
    }

- math July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There can no space in height, width or length.

function numberOfChocolates(a,b,c) {
    if ( (a%2) != 0 ) return 0;
    if ( (b%2) != 0 ) return 0;
    if ( (c%2) != 0 ) return 0;
  
    int heightNum = a/2;
    int widthNum = b/2;
    int lengthNum = c/2;
    return heightNum * widthNum * lengthNum;   


}

- stephenpince July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. What is the individual dimension on each chocolate ? L x b x h? It can 1x1x2 or cuberoot(2) ^3. All solutions discussed are assuming each side is of dimension 2 which is not the case from the question atleast.
2. Are all chocolates are of same dimension or different dimension ? [they could still have same volume]

- vaidehi.venkatesan October 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// input = box width, box height, box depth
// output = number of chocolates that fit
int howManyFit(int boxW, int boxH, int boxD) {
	return Math.min(Math.min(boxW/2, boxH/2), boxD/2);
}

- Anonymous January 20, 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