GE (General Electric) Interview Question
Software Engineer / Developers<pre lang="java" line="1" title="CodeMonkey76136" class="run-this">/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
*
Getting directions would be done as follows:
*
* 1. A stack of unvisited locations is created
* 2. All subway locations are added to the unvisited location stack.
* 3. A stack of travelers is created
* 4. A single traveler is added to that stack and is set to startLocation
* 5. For each traveler in the travelers stack every subway route is looped throught
* 6. For each route all stops are looped througth
* 7. If a stop is found that is unvisited and adjacent to a
* traveler's location then a copy of the traveler is added to that location.
* That new location is also added to the new travelers travel history.
* The location is removed from the unvisited location stack. *
* If the traveler is added to a location that is positioned lower than the previous
location then a backschedule is used to compute schedule time
otherwise a forward schedule is used.
The travel time and schedule history is updated by that traveler.
* 8. Finally if the traveler finds the endLocation a traveler is created at
* that location and returned (with the arraylist of time and locations).
* 9. If a traveler cannot go to any unvisited locations anymore it is
* removed from the traveler stack.
*/
import java.util.ArrayList;
/**
*
* @author djoshi
*/
public class SubwayTest {
class subwaySystem{
private ArrayList<Location>stops;
private ArrayList<Route>routes;
subwaySystem(ArrayList<Location>stops, ArrayList<Route>routes){
this.stops=stops;
this.routes=routes;
}
public ArrayList<Location>getStops(){
return stops;
}
public ArrayList<Route>getRoutes(){
return routes;
}
public void getDirections(Location startLocation,Location endLocation){
if(!(stops.contains(startLocation) &&stops.contains(endLocation))){
System.out.println("That Location Does Not Exist in this Subway!");
return;
}
}
}
class Traveler{
private ArrayList<Location>visitedLocations;
private ArrayList<Time>visitedTime;
private int duration;
Traveler(){
this.visitedLocations=new ArrayList<Location>();
this.duration=0;
}
Traveler(Traveler traveler, Location location, Time time, int duration){
this.visitedLocations.addAll(traveler.getVisitedLocations());
this.visitedLocations.add(location);
this.visitedTime.addAll(traveler.getVisitedTime());
this.visitedTime.add(time);
this.duration=this.duration+duration;
}
public ArrayList<Location>getVisitedLocations(){
return visitedLocations;
}
public ArrayList<Time>getVisitedTime(){
return visitedTime;
}
}
class Route{
private ArrayList<Location>stops;
private ArrayList <Schedule> forwardRouteSchedule;
private ArrayList <Schedule> backRouteSchedule;
Route(ArrayList<Location>stops, ArrayList <Schedule> forwardRouteSchedule,
ArrayList <Schedule> backRouteSchedule){
this.stops=stops;
this.forwardRouteSchedule = forwardRouteSchedule;
this.backRouteSchedule = backRouteSchedule;
}
public ArrayList<Location>getStops(){
return stops;
}
public Schedule getSchedule(int routeLocation, boolean forward){
if(forward){
return forwardRouteSchedule.get( routeLocation );
}else{
return backRouteSchedule.get(routeLocation);
}
}
}
class Schedule{
private ArrayList<Time>timeList;
Schedule(ArrayList<Time>timeList){
this.timeList=timeList;
}
public ArrayList<Time>getSchedule(){
return timeList;
}
}
class Time{
private int hour;
private int minute;
Time(int hour,int minute){
this.hour=hour;
this.minute=minute;
}
int getTimeDifference(Time laterTime){
return laterTime.getMinute()-this.getMinute()+(laterTime.getHour()-this.getHour())*60;
}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
}
class Location{
private String name;
Location(String name){
this.name=name;
}
public String getName(){
return name;
}
}
}
</pre><pre title="CodeMonkey76136" input="yes">
</pre>
1. A stack of unvisited locations is created
- dhaval0129 September 13, 20102. All subway locations are added to the unvisited location stack.
3. A stack of travelers is created
4. A single traveler is added to that stack and is set to start Location
5. For each traveler in the travelers stack every subway route is looped through
6. For each route all stops are looped through
7. If a stop is found that is unvisited and adjacent to a traveler's location then a copy of the traveler is added to that location.That new location is also added to the new travelers travel history. The location is removed from the unvisited location stack. If the traveler is added to a location that is positioned lower than the previous location then a back schedule is used to compute schedule time otherwise a forward schedule is used.The travel time and schedule history is updated by that traveler.
8. Finally if the traveler finds the end Location a traveler is created at that location and returned (with the array list of time and locations).
9. If a traveler cannot go to any unvisited locations anymore it is removed from the traveler stack.