Google Interview Question for Interns

Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
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.

- losmi April 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
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 {
  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;

  int a_;
  int b_;
  int weight_;

class EdgeWeightedGraph {
  EdgeWeightedGraph(int size)
  : adjacency_(size)

  void add_edge(int a, int b, int weight) {
    if(a < adjacency_.size() && b < adjacency_.size() && std::find(adjacency_[a].begin(), adjacency_[a].end(), WeightedEdge(a, b, weight)) == adjacency_[a].end()) {
      adjacency_[a].emplace_back(a, b, weight);
      adjacency_[b].emplace_back(a, b, weight);

  const std::vector<WeightedEdge>& adjacent(int a) const {
    return adjacency_[a];

  void print() const {
    for(std::size_t i = 0; i < adjacency_.size(); ++i)
      for(const auto& e : adjacency_[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) {
      for(const WeightedEdge& e : adjacency_[i])
        if(i < e.other(i)) res.push_back(e);
    return res;

  std::vector<int> dfs(int start) const {
    std::vector<bool> visited(adjacency_.size(), false);
    std::stack<int> nodes_stack;
    std::vector<int> res;


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

  std::size_t size() const {
    return adjacency_.size();

  std::vector<std::vector<WeightedEdge>> adjacency_;

int main() {
  EdgeWeightedGraph graph(6);

  graph.add_edge(0, 1, 1);
  graph.add_edge(0, 2, 2);
  graph.add_edge(0, 5, 3);
  graph.add_edge(2, 4, 4);
  graph.add_edge(3, 4, 5);
  graph.add_edge(3, 5, 6);
  graph.add_edge(4, 5, 7);

//  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;

- losmi April 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 1 vote

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

- Saad April 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
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

- sahil.070197 July 12, 2017 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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