Lunatic Server Solutions Interview Question for Java Developers


Country: United States




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

A simple Dijkstra's algorithm to find the shortest path between source node and destination node. Whenever the depth is greater than the height of the man, treat it as an invalid edge. Below is the lose implementation of the above. NOTE: Implementation could be done better by using adjacency list to represent the graph and using priority queue. Time complexity is O(Vlog(V) where, V is the number of vertices.

#include<iostream>
#include<list>
using namespace std;
#define INFINITY 10000;

int disjkstras(int **graph, int n, int source, int dest);
int get_min_vertex(int *distance, int *visited, int n);

int main() {
  int H;
  cin >> H;
  int E, N;
  cin >> E >> N;

  int **graph = new int*[N];
  for(int i=0; i<N ; i++) {
   graph[i] = new int[N];
  }

  for(int i=0; i<N; i++) {
   for(int j=0; j<N; j++) {
     if(i==j) {
       graph[i][j] = 0;
     } else {
       graph[i][j] = INFINITY;
     }
   }
  } 

  while(E--) {
    int C1, C2, T, D;
    cin >> C1 >> C2 >> T >> D;
    if(D <= H) {
      graph[C1-1][C2-1] = graph[C2-1][C1-1] = T;
    }
  }

  int S, E_;
  cin >> S >> E_;
  disjkstras(graph, N, S-1, E_-1);
 
}

int disjkstras(int **graph, int n, int source, int dest) {
  int *min_distance = new int[n]; 
  int *route = new int[n];
  int *visited = new int[n]; 

  for(int i=0; i<n; i++) {
      min_distance[i] = INFINITY;
      visited[i]=0;
      route[i] = -1;
  }
  min_distance[source] = 0;
  route[source] = source;


  for(int i=0; i<n ; i++) {
    int min_vertex = get_min_vertex(min_distance, visited, n);
    for(int j=0; j<n; j++) {
      if(min_distance[j] > (graph[min_vertex][j] + min_distance[min_vertex])) {  
        min_distance[j] = graph[min_vertex][j] + min_distance[min_vertex];
        route[j] = min_vertex;
      }
    }
    visited[min_vertex] = 1;
  }


  cout << "Minimum distance : " << min_distance[dest] << endl;
  cout << "Path is : " << endl;
  list<int> path;
  path.push_front(dest);
  for(int i=dest; ; ) {
    path.push_front(route[i]);
    if(route[i] == source) {
      break;
    } else {
      i = route[i];
    }
  }
      
  list<int>::iterator it;
  for(it=path.begin(); it!=path.end(); it++) {
    cout << (*it+1) << " ";
  }

  delete[] route;
  delete[] min_distance;
  delete[] visited;
}

int get_min_vertex(int *distance, int *visited, int n) {
  int ret_val = -1;
  int min = INFINITY;
  for(int i=0; i<n; i++) {
    if((distance[i] < min) && (visited[i] == 0)) {
      min = distance[i];
      ret_val = i;
    }
  }
  return(ret_val);
}

- Prakash May 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi prakash,
i am not familiar with the following code
list<int>::iterator it;
for(it=path.begin(); it!=path.end(); it++) {
cout << (*it+1) << " ";
can you suggest which compiler supports it and how

thank you

- dev May 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

On a UNIX platform, a g++ compiler must be able to compile the code.

- Prakash May 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do the roads continue flooding and get deeper as Mr. X travels? Otherwise this seems like too easy of a problem for a coding competition

- Anonymous May 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes you are right.
this one is the only twist with dijkstra algorithm.
try to find the solution in java.
you can also try for my previous post i.e. hash table insertion mapping and memory management problem.

- dev May 29, 2012 | Flag


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