Uber Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

4/5 is a graph problem - similar to finding the number of connected components in graph.
DFS solution:
In this graph every node has at most 2 edges. Every position (x, y) has 2 nodes. If it's a '/' in (x, y) and current node is upper half of (x,y), the next two nodes to search is right half of (x - 1, y) and lower half of (x, y - 1).
Other than DFS, union find and BFS will work as well.

public int segmentCount(char[][] m) {
        int len = m[0].length;
        boolean[] upperHalf = new boolean[m.length * len];
        boolean[] lowerHalf = new boolean[m.length * len];

        int count = 0;
        for(int i = 0; i < m.length; i++) {
            for(int j = 0; j < len; j++) {
                if(!upperHalf[i*len + j]) {
                    count++;
                    dfs(m, upperHalf, lowerHalf, i, j, 0);
                }
                if(!lowerHalf[i*len + j]) {
                    count++;
                    dfs(m, upperHalf, lowerHalf, i, j, 1);
                }
            }
        }
        return count;
    }
    //upper:0, lower:1, left:2, right:3
    private void dfs(char[][] m, boolean[] upperHalf, boolean[] lowerHalf, int x, int y, int position) {
        if(x < 0 || x == m.length || y == m[0].length || y < 0) {
            return;
        }
        if((position == 2 && m[x][y] == '\\') || (position == 3 && m[x][y] == '/')) position = 1;
        if((position == 2 && m[x][y] == '/') || position == 3 && m[x][y] == '\\') position = 0;
        int id = x * m[0].length + y;
        if((position == 0 && upperHalf[id]) || (position == 1 && lowerHalf[id])) { //if visited
            return;
        }
        if(position == 0) upperHalf[id] = true;
        else lowerHalf[id] = true;
        if(position == 0 && m[x][y] == '\\') {
            dfs(m, upperHalf, lowerHalf, x, y + 1, 2); //go right
            dfs(m, upperHalf, lowerHalf, x - 1, y, 1); //go up
        }
        if(position == 0 && m[x][y] == '/') {
            dfs(m, upperHalf, lowerHalf, x, y - 1, 3); //go left
            dfs(m, upperHalf, lowerHalf, x - 1, y, 1); //go up
        }
        if(position == 1 && m[x][y] == '\\') {
            dfs(m, upperHalf, lowerHalf, x, y - 1, 3); //go left
            dfs(m, upperHalf, lowerHalf, x + 1, y, 0); //go down
        }
        if(position == 1 && m[x][y] == '/') {
            dfs(m, upperHalf, lowerHalf, x, y + 1, 2); //go right
            dfs(m, upperHalf, lowerHalf, x + 1, y, 0); //go down
        }
    }

- aonecoding July 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please elaborate a bit. I find it difficult to visualize how this problem is similar to connected components problem ?

- Rahul November 26, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please elaborate a bit. I find it difficult to visualize how this problem is similar to connected component problem So how the DFS is applied.

Does the following diagram divides into 12 graphically pieces or just 4 ? I guess it must be 4. 

/ / /
/ / /
/ / /

- rahul.s November 26, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include<bits/stdc++.h> 
using namespace std;

void dfs(vector<set<int>>& graph, int node, int& result, vector<bool> visited) {
	if(visited[node]) return;
	
	visited[node] = true;
	set<int> temp(graph[node]);
	for(auto neighbor : temp) {
		if(graph[node].find(neighbor) == graph[node].end())
			continue;
		graph[node].erase(neighbor);
		graph[neighbor].erase(node);
		if(visited[neighbor]) {
			cout << "Node " << node << " Neighbor " << neighbor << endl;
			result++;
		}
		else {
			dfs(graph, neighbor, result, visited);
		}
	}
}

int main() {
	// your code goes here
	vector<string> input;
	for (std::string line; std::getline(std::cin, line);) {
        input.push_back(line);
    }
    
    //construct the graph
    int columns = input[0].size() + 1;
    int rows = input.size() + 1;
    
    int lastRowStartIndex = (rows-1)*columns;
    vector<set<int>> graph(rows*columns, set<int>());
    for(int i = 0; i < columns-1; i++) {
    	graph[i].insert(i+1);
    	graph[i+1].insert(i);
    	
    	graph[i + lastRowStartIndex].insert(i + lastRowStartIndex + 1);
    	graph[i + lastRowStartIndex + 1].insert(i + lastRowStartIndex);
    }
    
    for(int i = 0; i < lastRowStartIndex; i+=columns) {
    	graph[i].insert(i+columns);
    	graph[i+columns].insert(i);
    	
    	graph[i + columns - 1].insert(i+2*columns - 1);
    	graph[i + 2*columns - 1].insert(i + columns - 1);
    }
    
	int currentNode = 0;
    for(string line : input) {
    	// cout << line << endl;
    	for(char c : line) {
    		if(c == '\\') {
    			graph[currentNode].insert(currentNode + columns + 1);
    			graph[currentNode + columns + 1].insert(currentNode);
    		}
    		else {
    			graph[currentNode+1].insert(currentNode + columns);
    			graph[currentNode + columns].insert(currentNode+1);
    		}
    		currentNode++;
    	}
    	currentNode++;
    }
    
    int result = 0;
    vector<bool> visited(rows*columns, false);
    // cout << "Everything is fine" << endl;
    dfs(graph, 0, result, visited);
    
    cout << result << endl;
	return 0;
}

- Anonymous January 06, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include<bits/stdc++.h> 
using namespace std;

void dfs(vector<set<int>>& graph, int node, int& result, vector<bool> visited) {
	if(visited[node]) return;
	
	visited[node] = true;
	set<int> temp(graph[node]);
	for(auto neighbor : temp) {
		if(graph[node].find(neighbor) == graph[node].end())
			continue;
		graph[node].erase(neighbor);
		graph[neighbor].erase(node);
		if(visited[neighbor]) {
			cout << "Node " << node << " Neighbor " << neighbor << endl;
			result++;
		}
		else {
			dfs(graph, neighbor, result, visited);
		}
	}
}

int main() {
	// your code goes here
	vector<string> input;
	for (std::string line; std::getline(std::cin, line);) {
        input.push_back(line);
    }
    
    //construct the graph
    int columns = input[0].size() + 1;
    int rows = input.size() + 1;
    
    int lastRowStartIndex = (rows-1)*columns;
    vector<set<int>> graph(rows*columns, set<int>());
    for(int i = 0; i < columns-1; i++) {
    	graph[i].insert(i+1);
    	graph[i+1].insert(i);
    	
    	graph[i + lastRowStartIndex].insert(i + lastRowStartIndex + 1);
    	graph[i + lastRowStartIndex + 1].insert(i + lastRowStartIndex);
    }
    
    for(int i = 0; i < lastRowStartIndex; i+=columns) {
    	graph[i].insert(i+columns);
    	graph[i+columns].insert(i);
    	
    	graph[i + columns - 1].insert(i+2*columns - 1);
    	graph[i + 2*columns - 1].insert(i + columns - 1);
    }
    
	int currentNode = 0;
    for(string line : input) {
    	// cout << line << endl;
    	for(char c : line) {
    		if(c == '\\') {
    			graph[currentNode].insert(currentNode + columns + 1);
    			graph[currentNode + columns + 1].insert(currentNode);
    		}
    		else {
    			graph[currentNode+1].insert(currentNode + columns);
    			graph[currentNode + columns].insert(currentNode+1);
    		}
    		currentNode++;
    	}
    	currentNode++;
    }
    
    int result = 0;
    vector<bool> visited(rows*columns, false);
    // cout << "Everything is fine" << endl;
    dfs(graph, 0, result, visited);
    
    cout << result << endl;
	return 0;
}

- thephenom January 06, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Y no comment

- thephenom January 06, 2020 | 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