Uber Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
Could you please elaborate a bit. I find it difficult to visualize how this problem is similar to connected components problem ?
#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;
}
#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;
}
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.
- aonecoding July 20, 2017