EMC Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

Classic application of recursion + backtracking.

My solution assumes that you are given a starting position in the maze, and that getting out of the maze is equivalent to reach a free position in the borders. With that in mind, all you have to do is branch on every direction (left, right, up, down), recursively finding the path until a border position is reached, or until you have tried every possibility and none worked (meaning it is impossible to get out of the maze from that starting position).

Just be careful to keep track of visited positions in a given path search, so that you don't recurse infinitely. Also, as positions are added to the path, we can keep track of the current path in a vector (C++ jargon for growable array), so that we can print it when we reach one of the border positions.

C++ implementation, tested and (apparently) working:

#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

bool find_path_aux(const vector<vector<char> > &maze, vector<vector<bool> > &visited,
		   vector<string> &path, int curr_x, int curr_y) {

	if (curr_x < 0 || curr_x >= (int) maze.size() || curr_y < 0 || curr_y >= (int) maze[0].size())
		return true;

	if (visited[curr_x][curr_y] || maze[curr_x][curr_y] == 'X')
		return false;

	assert(maze[curr_x][curr_y] == '_');

	visited[curr_x][curr_y] = true;
	ostringstream oss;
	oss << "( " << curr_x << ", " << curr_y << " )";
	path.push_back(oss.str());

	for (int i = -1; i < 2; i++)
		for (int j = -1; j < 2; j++)
			if ((i == 0) != (j == 0))
				if (find_path_aux(maze, visited, path, curr_x+i, curr_y+j))
					return true;

	path.pop_back();
	visited[curr_x][curr_y] = false;

	return false;
}

bool find_path(const vector<vector<char> > &maze, vector<string> &path, int pos_x, int pos_y) {
	path.clear();
	vector<vector<bool> > visited(maze.size(), vector<bool>(maze[0].size(), false));
	return find_path_aux(maze, visited, path, pos_x, pos_y);
}

int main(void) {
	cout << "Enter maze dimensions N and M for an NxM maze, followed by each entry separated by a blank," << endl;
	cout << "followed by the initial position" << endl;
	cout << "> ";

	unsigned rows, cols;
	while (cin >> rows >> cols) {
		vector<vector<char> > maze(rows, vector<char>(cols));
		for (unsigned i = 0; i < rows; i++)
			for (unsigned j = 0; j < cols; j++)
				cin >> maze[i][j];

		unsigned pos_x, pos_y;
		cin >> pos_x >> pos_y;

		vector<string> path;
		if (find_path(maze, path, pos_x, pos_y)) {
			if (path.size() > 0)
				cout << path[0];
			for (vector<string>::size_type i = 1; i < path.size(); i++)
				     cout << " -> " << path[i];
			     cout << endl;
		} else {
			cout << "Impossible" << endl;
		}

		cout << "> ";
	}

	return 0;
}

- 010010.bin August 21, 2015 | 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