## 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);
}``````

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

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.

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;
}``````

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

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

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

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 );

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));
}``````

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;

}``````

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]

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);
}``````

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.

### 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.