## Google Interview Question for Interns

• 1
of 1 vote

Interview Type: Phone Interview

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

Assuming that there cannot be two neighboring cliffs of the same height, as those shall be treated as the same cliff.

Model the island as a directed graph where each cliff is a node with edges pointing to neighboring cliffs of lower height. Now use DFS from the starting cliff to find all cliffs which are surrounded only by higher cliffs. Remember which cliffs you have already visited in order to save checking the same path after some "fork-merge". After each fork determine which path went to the lower cliff. At the end return the lowest cliff.

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

Here is a regular Weighted Digraph. It shouldn't be that hard to modify it so that it solves this problem.

``````#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

class WeightedEdge {
public:
WeightedEdge(int a, int b, int weight)
: a_(a), b_(b), weight_(weight)
{}

int either() const {
return a_;
}

int other(int some) const {
if(a_ == some)         return b_;
else if(b_ == some) return a_;
else                          return -1;
}

int weight() const {
return weight_;
}

bool operator==(const WeightedEdge& other) const {
return (a_ == other.a_ && b_ == other.b_ && weight_ == other.weight_)
|| (a_ == other.b_ && b_ == other.a_ && weight_ == other.weight_);
}

bool operator<(const WeightedEdge& other) const {
return weight_ < other.weight_;
}

bool operator>(const WeightedEdge& other) const {
return weight_ > other.weight_;
}

WeightedEdge& operator=(const WeightedEdge& other) {
a_ = other.a_;
b_ = other.b_;
weight_ = other.weight_;
return *this;
}

private:
int a_;
int b_;
int weight_;
};

class EdgeWeightedGraph {
public:
EdgeWeightedGraph(int size)
{}

void add_edge(int a, int b, int weight) {
}
}

const std::vector<WeightedEdge>& adjacent(int a) const {
}

void print() const {
for(std::size_t i = 0; i < adjacency_.size(); ++i)
std::cout << i << " " << e.other(i) << " " << e.weight() << std::endl;
}

std::vector<WeightedEdge> edges() const {
std::vector<WeightedEdge> res;
for(std::size_t i = 0; i < adjacency_.size(); ++i) {
if(i < e.other(i)) res.push_back(e);
}
return res;
}

std::vector<int> dfs(int start) const {
std::stack<int> nodes_stack;
std::vector<int> res;

nodes_stack.push(start);

while(!nodes_stack.empty()) {
int curr = nodes_stack.top();
nodes_stack.pop();
if(!visited[curr]) {
res.push_back(curr);
visited[curr] = true;
for(const auto& e : adjacent(curr)) {
if(!visited[e.other(curr)])
nodes_stack.push(e.other(curr));
}
}
}
return res;
}

std::size_t size() const {
}

private:
};

int main() {
EdgeWeightedGraph graph(6);

//  graph.print();

//  for(int i : graph.dfs(0))
//    std::cout << i << std::endl;

for(const WeightedEdge& e : graph.edges())
std::cout << e.either() << " " << e.other(e.either()) << " " << e.weight() << std::endl;

return 0;
}``````

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

Thanks that makes sense. Can you write a sample solution for it or direct me to a similar question/solution?

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

Assuming that island is represented as a 2d matrix "Isl", Isl[i][j] will denote the height of the cliff at point x,y on the island, given the starting point of the droplet, we can do a bfs, visiting valid neighbours of less or equal heights, storing the visited point i,j in a seperate matrix. Finally find the minimum height amongst the visited vertices will give answer.
Its simple to implement, just normal bfs

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.

### 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.