Google Interview Question for SDE1s


Country: United States




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

Please define the problem again. It's not describe correctly.

Guess : Given a proposed plan for flights against crew member schedule, whether it's possible or not, is a problem statement

- Ambiguous December 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.google.test;

import java.util.Scanner;

public class FlightCrewAvailability
{

    public static void main(String[] args)
    {
        Scanner inputScanner = new Scanner(System.in);
        int totalflights = inputScanner.nextInt();
        inputScanner.nextLine();
        Flight[] flights = new Flight[totalflights];
        for(int i=0;i<totalflights;i++)
        {
            String start = inputScanner.next();
            String end = inputScanner.next();
            int starttime = inputScanner.nextInt();
            int endtime = inputScanner.nextInt();            
            flights[i] = new Flight(start, end, starttime, endtime);
            inputScanner.nextLine();
        }
        int totalCrews = inputScanner.nextInt();
        inputScanner.nextLine();
        Crew[] crews = new Crew[totalCrews];
        for(int i=0;i<totalCrews;i++)
        {
            String site = inputScanner.nextLine();            
            crews[i] = new Crew(site);
        }
        for(int i=0;i<flights.length;i++)
        {
            for(int j= i +1 ;j<flights.length;j++)
            {
                if(flights[i].starttime > flights[j].starttime)
                {
                    Flight temp = flights[j];
                    flights[j] = flights[i];
                    flights[i] = temp;
                }
            }
        }
        boolean isCrewAvailable = true;
        for (Flight flight : flights)
        {
            boolean crewForFlight = false;
            for(Crew crew : crews)
            {
                if(crew.site[flight.starttime].equals(flight.start) && crew.status[flight.starttime] && 
                        crew.site[flight.endtime].equals(flight.start) && crew.status[flight.endtime])
                {
                    int i = flight.starttime;
                    while(i< flight.endtime)
                    {
                        crew.site[i] = flight.start;
                        crew.status[i] = false;
                        i++;
                        
                    }
                    i = flight.endtime;
                    while(i< crew.site.length && crew.status[i])
                    {
                        crew.site[i] = flight.end;
                        i++;
                    }
                    crewForFlight = true;
                    break;
                }
                
            }
            if(!crewForFlight)
            {
                isCrewAvailable = false;
                break;
            }
            
        }
        System.out.println(isCrewAvailable);
        
        inputScanner.close();
    }
    static class Crew {
        private String[] site;
        private boolean[] status;
        public Crew(String origin)
        {
            site = new String[24];
            status = new boolean[24];
            for(int i=0;i<24;i++)
            {
                site[i] = origin;
                status[i] = true;
            }
        }
    }
    static class Flight {
        
        private String start;
        private String end;
        private int starttime;
        private int endtime;
        public Flight(String start,String end,int starttime,int endtime)
        {
            this.start = start;
            this.end = end;
            this.starttime = starttime;
            this.endtime = endtime;
        }
    }
}

- MADHU SAMPANGI PRAKASH December 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I did not sort the flights for any locations. Instead I used a 2d vector where rows are the locations and columns are the time 0-23. used a hashmap to keep track of the location and corresponding row number in the 2d vector. After inserting all the flight and crew information (added 1 at time: 0 for the crew list, added 1 at arrival time for destination location and subtracted 1 at start_time for start location). Finally updated the 2d vector with 2d vector[i][j]= 2dvector[i][j]+2dvector[i][j-1].

struct flight{
	char start_location, destination_location;
	int start_time, arrival_time;
};
bool is_exist(hash_map<char, int> hm, char l){
	hash_map<char, int>::const_iterator it=hm.find(l);
	if(it==hm.end()){
		return false;
	}else{
		return true;
	}
}
void add_the_location_in_hm_and_update_vector(hash_map <char, int>& hm, char l, vector<vector<int>>& location_crew, int row_number){
	typedef pair <char,int>ipair;
	hm.insert(ipair(l,row_number));
	location_crew.resize(row_number+1); location_crew[row_number].resize(24);
	for(int i=0; i<24; i++){
		location_crew[row_number][i]=0;
	}
}
bool is_possible(vector<flight> list_of_flights, vector<char>crew_list){
	vector<vector<int>> location_crew;
	hash_map <char, int>hm;
	
	int row_number =0;
	for(int i=0; i<crew_list.size(); i++){
		if(is_exist(hm, crew_list[i])){
			int r=hm[crew_list[i]];
			location_crew[r][0]++;
		}else{
			add_the_location_in_hm_and_update_vector(hm, crew_list[i], location_crew, row_number);
			location_crew[row_number][0]++;
			row_number++;
		}
	}
	for(int i=0; i<list_of_flights.size(); i++){
		if(is_exist(hm, list_of_flights[i].start_location )){
			int r=hm[list_of_flights[i].start_location];
			location_crew[r][list_of_flights[i].start_time]--;
		}else{
			 add_the_location_in_hm_and_update_vector(hm, list_of_flights[i].start_location, location_crew, row_number);
			 location_crew[row_number][list_of_flights[i].start_time]--;
			 row_number++;
		}
		if(is_exist(hm, list_of_flights[i].destination_location )){
			int r=hm[list_of_flights[i].destination_location];
			location_crew[r][list_of_flights[i].arrival_time]++;
		}else{
			 add_the_location_in_hm_and_update_vector(hm, list_of_flights[i].destination_location, location_crew, row_number);
			 location_crew[row_number][list_of_flights[i].arrival_time]++;
			 row_number++;
		}
	}
	for(int i=0; i<location_crew.size(); i++){
		if (location_crew[i][0]<0)return false;
		for(int j=1; j<location_crew[i].size(); j++){
			location_crew[i][j]=location_crew[i][j]+location_crew[i][j-1];
			if(location_crew[i][j]<0) return false;
		}
	}
	return true;

}

- Anonymous December 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort every time point including departure time and arrival time.
Then scan all the time point. Firstly, drop off crews for arrival flights. Then let some crews go to departure flights. If no enough crews, then return false result.

struct Flight{
    string start;
    string destination;
    int start_time;
    int arrive_time;
    Flight(string s, string d, int st, int ar){
        start = s;
        destination = d;
        start_time = st;
        arrive_time = ar;
    }
};

bool check(vector<Flight> flights, vector<string> crews){
    vector<string> arrive[25];
    vector<string> departs[25];
    map<string, int> crew_in_place;
    for (int i = 0; i < crews.size(); ++i){
        crew_in_place[crews[i]]++;
    }
    for (int i = 0; i < flights.size(); ++i){
        arrive[flights[i].arrive_time].push_back(flights[i].destination);
        departs[flights[i].start_time].push_back(flights[i].start);
    }
    for (int i = 0; i < 25; ++i){
        for (int j = 0; j < arrive[i].size(); ++j){
            crew_in_place[arrive[i][j]]++;
        }
        for (int j = 0; j < departs[i].size(); ++j){
            crew_in_place[departs[i][j]]--;
            if (crew_in_place[departs[i][j]] < 0){
                return false;
            }
        }
    }
    return true;
}

- lxfuhuo January 07, 2018 | Flag Reply


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