Microsoft Interview Question for Software Engineer / Developers


Team: Windows
Country: United States




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

Use backtracking with a 3D solution matrix.
Terminating condition :
if(x == 0 || x == n-1 || y == 0 || y == n-1 || z== 0 || z== n-1)
At this pt. print the sol matrix and backtrack to find other possible paths.

- Srikant Aggarwal October 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution.

Let f(n) be the function which does the job. That is, returns all paths from the centre of the cube to the surface. Then, f(n) can be constructed from f(n-2). If you have all paths returned by f(n-2), looks at each path. Look at the last vertex on the path. See which coordinate/s of the last vertex can be extended by one step to reach 0 or n. You can only change one coordinate in one step. Once any coordinate hits n OR 0, we are done. So, create more paths from the paths returned by f(n-2) in this way. Keep doing this till you reach the base case. which will be f(3) or f(2). Both of them are easy to solve.

- alphalpha October 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Slight mistake. It's better to assume we are at the origin and that n = 2m or 2m+1. So end points will be m and -m, say. (for the odd case at least)

Then, f(n-2) returns path in which last vertices have one vertex at m-1 or -m+1.

- alphalpha October 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Apply single-source shortest path from the center to all vertices. Then run through the edge vertices e.g. (x == 0 || x == n-1 || y == 0 || y == n-1 || z== 0 || z== n-1) and generate the sequence of path from that vertex to the center for all those vertices.

Use Dijkstra's algorithm with Min-Heap.

I hope this should solve the problem as I am touching all the vertices on the edge.

- Dev October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in Question itz mentioned like we need to go to surface not specifically to vertices....so y shld we go to vertices as shorter path is there from starting point to the surface of each face

- atul gupta October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well you are given only the vertices of the cube, so can you please tell me how will you reach the surface without going to one of the vertices?

- Dev October 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please write a question with proper punctuation marks. It requires to be read at least a couple of times to understand.

- Shyam October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Posting for feedback on implementation.

final int MAX_SIZE = 10; // Can set to whatever you want.
final int cube[][][] = new int[MAX_SIZE][MAX_SIZE][MAX_SIZE];
void printPathToSurface(int x, int y, int z){ // input can be the center location
 printPaths(x, y, z, "");
}
void printPaths(int x, int y, int z, String prevPath){
 if(x < 0 || y < 0 || z < 0 || x >= (MAX_SIZE) || y >= (MAX_SIZE) || z >= (MAX_SIZE))
  return;
 if(x == 0 || x == (MAX_SIZE - 1) || y == 0 || y == (MAX_SIZE - 1) ||
    z == 0 || z== (MAX_SIZE - 1)){
  System.out.println(prevPath);
  return;
 String path = new String(prevPath);
 path += "(" + String.valueof(cube[x]) + "," + String.valueof(cube[y]) + "," +
                String.valueof(cube[z]) + ")"
 for(int i=x-1; i<=x+1;i++){
  for(int j=y-1; j<=y+1;j++){
   for(int k=z-1; k<=z+1;k++){
    printPaths(i, j, k, path);
   }
  }
 }
}

- CodeSpace October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# The interpretation of the problem here is that you are at the
# center of a cube, and it's n units from the center to the surface,
# and you want to enumerate all paths that go to a point on the surface
# without reversing direction in any dimension.  Diagonal moves
# aren't allowed; every step in the path is magnitude 1 and 
# parallel to one of the three axes.  Paths are allowed to continue
# along the surface, as long as you don't reverse directions.

directions = dict(
    L = ('x', -1),
    R = ('x', 1),
    D = ('y', -1),
    U = ('y', 1),
    B = ('z', -1),
    F = ('z', 1),
)
    
def generate_paths(n):
    def gen_paths(path, coords):
        if any(abs(coord) > n for coord in coords.values()):
            return
        if any(abs(coord) == n for coord in coords.values()):
            yield path
        for direction, delta_info in directions.items():
            axis, delta = delta_info
            # make sure we don't backtrack, take advantage
            # that product of two negative numbers is positive
            if delta * coords.get(axis, 0) >= 0:
                new_path = path + direction
                new_coords = coords.copy()
                new_coords[axis] = new_coords.get(axis, 0) + 1
                for newer_path in gen_paths(new_path, new_coords):
                    yield newer_path

    return gen_paths('', {})            

def run():
    for n in [1,2,3,4]:
        solutions = generate_paths(n)
        num_solutions = sum(1 for solution in solutions)
        print 'n=%d: %d solutions' % (n, num_solutions)

run()

n=1: 78 solutions
n=2: 1878 solutions
n=3: 39222 solutions
n=4: 837846 solutions

- Steve October 27, 2012 | 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