Yahoo Interview Question
Software Engineer / DevelopersNice solution, but what's the rationale here? I tabulated for k=3 and n from 1 to 5 and got:
n | cubes | hidden cubes
1 1 0
2 8 0
3 27 1
4 64 8
5 125 27
You can clearly 'see' the relationship (n-2). But how one can arrive to this in a more smart way?
lets have n=1;
this is trivial 1 surface exposed;
n=2 here we have all 8 cubes exposed;
n=3 here we have one cube innermost cube jiddedn and rest all exposed;
so we have by symmetry following relations
n*n*n - (n-2)*(n-2)*(n-2)... cubes on surfacce as all the rest would be innermost ones. ,,
however what on earth does k dimension mean here..
1D: Object=line segment, surface=2 ends.
n small line segments, n-2 inner ones.
n - (n-2) = 2 mini-segments exposed.
2D: Object = (filled) square, surface = square outline.
n^2 small squares, (n-2)^2 inner squares.
n^2 - (n-2)^2 mini-squares exposed.
3D: Object = cube
n^3 - (n-2)^3
kD: n^k - (n-2)^k
Simple :-)
for 3d - (n^3 - (n-2)^3)
- dp September 18, 2010for kd - (n^k - (n-2)^k)