Interview Question for Program Managers


Country: United States




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

create the adjacency matrix from the given pairs, build all possible pairs (unidirectional) and add to result if no edge is present. In Java

public class RoadNetwork
{
    public static int[][] RoadBuilder(int nCities, int[][] builtRoads) {
        List<int[]> result = new ArrayList<>();
        boolean[][] adjMatrix = new boolean[nCities][nCities];
        for(int[] pair : builtRoads) {
            adjMatrix[pair[0]][pair[1]] = true;
            adjMatrix[pair[1]][pair[0]] = true;
        }
        for(int u = 0; u < nCities; u++) {
            for(int v = u + 1; v < nCities; v++) {
                if(!adjMatrix[u][v]) {
                    result.add(new int[]{u, v});
                }
            }
        }
        return result.toArray(new int[result.size()][2]);
    }

    public static void main(String[] args) {
        int[][] test1 = new int[][]{{0, 1}, {1, 2}, {3, 2}};
        System.out.println(
                Arrays.deepToString(
                        RoadBuilder(4, test1))); // expected result should be {{0,2}, {0, 3}, {1, 3}}
    }
}

- Chris May 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@sri The complexities are O(n^2). Temporally this can be explained as we can solve the problem by traversing the matrix, even if you can do it by traversing just half of it. Spatially is easy to see using the worst case in which there is no preexisting connection between the cities. In that case you would need to generate n-1 + n-2 ..... + 1 connections which has a quadratic cost. Basically the question is asking you to generate a clique, you can research the topic to get a deeper understanding.
Solution in Python:

def RoadBuilder(nCities, builtRoads):
    solutions = set()
    m = [ [0] * nCities for _ in xrange(nCities) ]
    for x,y in builtRoads:
        m[x][y] = 1
        m[y][x] = 1
    
    for x in xrange(nCities):
        for y in xrange(x+1, nCities):
            if m[x][y] == 0:
                solutions.add((x, y))
                
    return solutions

- Fernando May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what are the time and space complexities?

- sri May 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
#include <vector>

vector<pair<int, int> > build_roads(int n, vector<pair<int, int> > in) {
    vector<vector<int> > matrix(n, vector<int>(n));
    for(vector<pair<int, int> >::iterator it = in.begin(); it != in.end(); ++it) {
        // check wheth the it->first and it->second within the limits
        matrix[it->first][it->second] = 1;
    }
    
    vector<pair<int, int> > out;
    for(int i =0; i < n; i++) {
        for(int j=i+1; j<n; j++) {
            if (!matrix[i][j]) {
                out.push_back(pair<int,int>(i,j));
            }
        }
    }
    return out;
}
int main() {
	// your code goes here
	int ara[][2] = {{0,1}, {1,2}, {2,3}};
	vector<pair<int, int> > in;
	for(int k = 0; k < sizeof(ara)/sizeof(ara[0]); k++) {
	    in.push_back(pair<int, int>(ara[k][0], ara[k][1]));
	}
	vector<pair<int, int> > result = build_roads(4, in);
	for(vector<pair<int, int> >::iterator it = result.begin(); it != result.end(); ++it) {
	    cout << "{" << it->first<<"," <<it->second<<"}\n";
	}
	return 0;
}

- ali.kheam May 29, 2017 | 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