## Zynga Interview Question

Software Development Managers**Country:**United States

**Interview Type:**In-Person

I get the same answer as rob but I start out a little differently, rather then thinking about just the 6 faces and then subtracting edges for other face pairs as mentioned by rob you can calculate the volume of the cube and then subtract the volume of a cube that is one layer smaller i.e. fit inside the problem cube.

Note that if n == 1 then the answer is 1

for n > 1 -> outer sub cubes = volume of cube - volume of inner cube

= n^3 - (n-2)^3

this simplifies to 6n^2 - 12n + 8

```
// Given a cube made of N x N x N sub-cubes, how many sub-cubes are
// on the outside of the cube?
// Consider the trivial case N = 1, the answer is 1.
// Consider the next trivial case N = 2
// The answer is 8 because the 2x2x2 cube is comprised of only
// 8 subcubes, 1 @ each corner.
// Consider N = 3, we have 8 corner cubes, plus 1 cube in the
// center of each of the composite cube's 6 faces, plus 1 cube
// extra along the length of each of the main cube's 12 edges.
// Consider N = 4: 8 corner cubes, plus 4 cubes in the center
// of each face, plus the cubes between the corners on each of
// the 12 sides is 2.
// It turns out that this can be described by the following
// equation:
// ƒ(N) = 8 corner cubes + (6 faces * (N - 2)^2) + 12 sides * (N - 2)
"use strict";
module.exports = function (N) {
let sides = 0;
if (N === 1) {
sides = 1;
} else if (N > 1) {
sides = 8 + (6 * Math.pow(N-2, 2)) + (12 * (N - 2));
}
return sides;
};
```

cube has 6 faces, each has n*n subcubes on each face. so from top and bottom 2n^2+4(n-1)

Not quite but you seem to be on the right path. It looks like you tried to account for the 4 remaining sides with 4(n-1), which isn't correct.

By the way, what the interviewer also looked for is whether I was able to reduce the equation down to the simplest terms.

Note this interview I had was 2 years ago.

The way to solve this problem is to realize that for a cube to be contained inside another it has to be placed right in the middle, thus it has to measure two units less than the big cube (N-2) so it can fit.

Picture now the number of cubes (the volume) that will not "see the light" (N-2)^3 subtract this from the total N^3 and you'll have your "shell volume".

Very harsh question to ask under pressure and in the context of an interview... I guess there are worst things in life, so just do your best.

Method 1:

lowermost layer = n*n

uppermost layer = n*n

in between = (n-2)*(4*(n-1)) = (n-2)*(4n-4)=4*n*n -12n +8

Total = 6*n*n -12n +8

Method 2:

Total exposed cube surfaces on one flat surface = n*n

Total exposed cubes surfaces on 6 flat surfaces = 6*n*n ---(1)

No. of surfaces having another exposed surface in its cube twice or more times = No. of edges * cubes/edge = 12 * n ---(2)

No. of surfaces having 3 exposed surfaces in it's cube = No. of cornwes =8 ---(3)

Total no. of exposed cubes = (1) - (2) + (3)

= 6*n*n -12n +8

I reduced my equation to 6n^2 - 12n + 8.

- rob September 29, 20156n^2 is number of sub cubes w/o accounting for overlapping sides and corners.

Subtract 12n, 12 is the number of sides and n is the length of each side. Each side is accounted for twice in 6n^2.

Subtract 8, the number of corners, which are accounted for 3 times in 6n^2.