What is a path through a cube? This seems like some weird combination of graph theory and geometry.
What is a path through a cube? This seems like some weird combination of graph theory and geometry.
You could of course write a program that would solve it (trivially), but it might take exponential time :)
Of course, the phrasing doesn't say they have to be lattice paths, so perhaps we can say a countably infinite number of paths of we're only considering integral (or rational) points and have no direction invariant. Uncountably many if we're allowing the reals. Still uncountably many if we allow the reals and have a directional invariant. We reach the realm of a finite solution if it's a finite set of points and we have a directional invariant or another constraint (e.g., the path might be prohibited from visiting any given point more than once). Most of these are still completely intractable as far as I know :)
To count paths in 2-space from (0,0) to (5,6), you have the operations "X++" and "Y++" which go right and up, respectively. Each increasing path from (0,0) to (5,6) has to be some permutation of 5 times X++ and 6 times Y++. So the count is (5+6)!/(5!6!) where the exclamation mark denotes factorial. This extends to higher dimensions by adding an additional operation "Z++". Then the count to go from (0,0,0) to (5,6,7) is (5+6+7)!/(5!6!7!).
I think you can easily write the recursion:
p(x,y,z) = p(x-1,y,z) + p(x,y-1,z) + p(x,y,z-1)
Leave out the term with x-1, y-1, or z-1 for the edge cases for x, y, and/or z equal to zero.With that in hand, it is easy to compute all values bottom up, starting with those where x+y+z = 0, 1, 2, etc.
Definitely fewer than (x+y+z)^3 values to compute, all of them in O(1) (disregarding cases where the numbers become bignums)
Generalization to any number of dimensions seems easy, too.
A closed form solution, that might be harder.
Basically, yes, self-avoiding paths.
This was basically a brief aside in her honors undergrad algorithms course, so the topic was a bit beyond what I was prepared for at the time :)
Counting self-avoiding walks is hard in 2D, too (http://oeis.org/A007764)
But as best as I can tell, they're considering each 1x1x1 space to be a node like a Rubix cube, and they want a list of all possible paths from the center node to the surface. I imagine that half of the question is making sure that the interviewee presses for details, because there's plenty of problems I see with the question right off the bat. If n is even, there's no single center node, so where does the algorithm start? Can the algorithm traverse diagonally by edges, or only by adjacent faces? Not to mention that there's an infinite number of possible paths for some values of n, assuming paths can cross themselves, for the same reason that there's an infinite number of paths from my front door to my car if I feel like walking in circles for a while.
In this cube land question you can answear, how are you defining the centre and just revering that process will already give you the code you require. What they are doing you don't know so you have to ask, may be they are trying to reinvent a wheel and with that the best answear may be how to draw a circle as there question is flawed. This is the problem with made up interview questions, if they are based upon real world experience then you get a good question that you can truely answear. You may have a better answear or approach which with them having lived it, makes enough sence to know you would of saved them 2 days debugging that problem and thats from a quick chat walking of the street. If it is a made up question then your approach and alternative answear can be missed and ignored and your genius is not appreicieated.