CCN Interview Question for Applications Developers


Country: United States
Interview Type: Written Test




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

public static void printPaths(int mat[][], int s, int t, int n,ArrayList<Integer> list) {
		
		if (list.contains(new Integer(t + ""))) {
			System.out.println(list);
			return;
		}
		
		for (int i = 0; i < n; i++) {
			if (mat[s][i] == 1) {
				list.add(new Integer(i));
				mat[s][i] = 0;
				mat[i][s]=0;
				printPaths(mat, i, t, n, list);
				list.remove((Object) i);
				mat[s][i] = 1;
				mat[i][s]=1;
			}
		}
	}

	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		int n = new Integer(s);
		int mat[][] = new int[n][n];

		for (int i = 0; i < n; i++) {
			s = br.readLine();
			String arr[] = s.split(" ");
			for (int j = 0; j < n; j++)
				mat[i][j] = new Integer(arr[j]);
		}
		Integer s1=1,t1=0;
		ArrayList list=new ArrayList<Integer>();
		list.add(s1);
		printPaths(mat, s1, t1, n, list);
	}

* code performs DFS and checks for all possible paths between s and t .
* mat is the sqaure matrix of size n*n.
* list in recursion maintains all the paths

- salvo4u May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

think of Binary Tree kind of structure. where 'a' will be the root. 'b' and 'c' can be right and left nodes of it. and 'd' can be child of both...but i am not sure if this leads to the right answer...any thoughts ???

- Mario May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use a adjacency matrix for this. Now whichever nodes we have traversed we can mark them as done so that we do not visit them again

- DashDash May 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ code:
DFS, marks nodes as visited when going in, unvisited when going out, stops at destination node and outputs path.

#include <string>
#include <list>
#include <iostream>

class Node;
class Node
{
public:
    std::string name;
    std::list<Node*> adjacent; // list of adjacent nodes
    bool visited;
};

void search_path ( Node& node, Node& destination, std::string path )
{
    if ( node.visited )
        return;
    
    if ( &node == &destination )
    {
        std::cout << path + node.name << std::endl;
        return;
    }
    
    node.visited = true;
    
    std::string path_so_far = path + node.name + "--";
    
    for ( std::list<Node*>::iterator iter = node.adjacent.begin(); iter!=node.adjacent.end(); iter++ )
    {
        search_path ( **iter, destination, path_so_far );
    }
    
    node.visited = false;
}

int main()
{
    //Construct nodes and connect them + initialize their member "visited" to false; 
    //....
   
    search_path(node1,node2,"");
    
    return 0;
}

- oos September 13, 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