Booking.com Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

import java.util.*;
public class Solution {
	public  static void main (String args[]) {
		Solution r = new Solution();
		r.addHotel("WilliamHotel", 4);
		r.addHotel("RomaHotel", 10);
		r.addTransation(1, 2, "WilliamHotel");
		r.addTransation(1, 4, "RomaHotel");
		r.addTransation(2, 5, "RomaHotel");
		r.solve(1, 19);
	}
	private Map<String, Integer> hotels;
	private List<Transaction> transactions;
	public Solution() {
		this.hotels = new HashMap<>();
		this.transactions = new LinkedList<>();
	}
	public void addHotel(String hotelName, int numberOfRooms) {
		this.hotels.put(hotelName, numberOfRooms);
	}
	public void addTransation(int startDate, int endDate, String hotelName) {
		this.transactions.add(new Transaction(startDate, endDate, hotelName));
	}
	public void solve(int startDate, int endDate) {
		Map<String, Integer> hotelsDuplicate = new HashMap<>(hotels);
		for (Transaction t : transactions) {
			if(!notConflicts(t, startDate, endDate)) {
				hotelsDuplicate.put(t.hotelName, hotelsDuplicate.get(t.hotelName)-1);
			}
		}
		Iterator<String> iterator = hotelsDuplicate.keySet().iterator();
		while(iterator.hasNext()) {
			String h = iterator.next();
			System.out.println(h+" has "+hotelsDuplicate.get(h)+" room(s)");
		}
	}
	private boolean notConflicts(Transaction t, int startDate, int endDate) {
		if(endDate <= t.startDate || startDate >= t.endDate)
			return true;
		return false;
	}
	class Transaction {
		int startDate, endDate;
		String hotelName;
		public Transaction(int startDate, int endDate, String hotelName) {
			this.startDate = startDate;
			this.endDate = endDate;
			this.hotelName = hotelName;
		}
	}
}

- w.kinaan September 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution:
http://pastebin.com/zfeDGTPU

- Alisa October 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you get an onsite interview?

- Jack October 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you get an onsite interview?

- Jack October 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please provide a solution for this in java.

- Nobitha March 22, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class HotelAvailabilityCalculator

  attr_accessor :hotels_file_name, :bookings_file_name, :hotel_availability_hash

  def initialize(hotels_file_name, bookings_file_name)
    self.hotels_file_name = hotels_file_name
    self.bookings_file_name = bookings_file_name

    build_availability_hash
  end

  def build_availability_hash
    self.hotel_availability_hash = {}

    CSV.foreach(hotels_file_name, "rb") do |row|
      hotel_availability_hash[row[0]] = Hash.new(row[1].to_i)
    end

    CSV.foreach(bookings_file_name, "rb") do |row|
      (row[1].to_date...row[2].to_date).each do |stay_date|
        hotel_availability_hash[row[0]][stay_date] -= 1
      end
    end

    puts hotel_availability_hash.inspect
  end

  def available_hotels(checkin_date, checkout_date)
    available_hotels_list = []

    hotel_availability_hash.each do |hotel_name, availability_hash|
      available_hotels_list << hotel_name if hotel_available?(hotel_name, checkin_date, checkout_date)
    end

    available_hotels_list
  end

  def hotel_available?(hotel_name, checkin_date, checkout_date)
    (checkin_date...checkout_date).each do |stay_date|
      return false unless hotel_availability_hash[hotel_name][stay_date] > 0
    end

    true
  end

end

hac = HotelAvailabilityCalculator.new('hotels.csv', 'bookings.csv')

hac.available_hotels("2015-04-02".to_date, "2015-04-05".to_date)

- Sub September 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please,
Can anyone convert this to java.

- Nobitha March 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I got a solution - in java
make two hashmaps

Map<String,Integer> hotels = new HashMap();

//read data from hotels.csv and save in this hashmap.

Map<String,Map<Date,Integer>> bookings = new HashMap();

// in this hash map read data from bookings.csv and fill data if inner hashmap don't exist for a date read data from hotels hashmap and fill in bookings hashmap, during search if bookings hashmap has that date data then print it else all rooms are available read from hotels hash map and show.

- Anonymous May 11, 2017 | 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