Help with booking algorithm question




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

you need to do a map reduce program, i would do it in bash+python

your map function gets the booking.csv file emits a line as follow:
key: (hotel, start date, end date), value: 1
your reduce function gets all the keys and does sum of the values, and checks it with the file hotels.csv
it emits a line if the sum you got is smaller then the sum in file.
the line will look like this:
hotel, start date, end date

you can do another phase of aggregating the sequence when possible.

the program is as follow:

available_rooms.sh:
-------------------------
#!/bin/bash
# code receives the two files you mentioned
booking_file_path = $1
hotels_file_path = $2

cat booking_file_path | python map_function.py | sort | python reduce_function.py $hotels_file_path

the map_function code is:
-----------
try:
for line in sys.stdin:

if not line or not line.strip():
continue
line = line.strip()
print line+":"+str(1)

except "end of file":
return None

---------
the reduce_function code is:

def reduce_phase(file_path):
last_key, key_sum = None
try:
for line in sys.stdin:
if not line or not line.strip():
continue

line = line.strip()
key,delimiter,value = line.split()

if not last_key:
last_key = key

# if this is the same key as in the previous read line
if last_key == key:
# accumulate
key_sum += 1
# if this is a new key, check from the file the total available space, check it with the
# current sum and emit a line if there is capacity available
else:
check_and_print_result(key, key_sum)
last_key = key

# last key
check_and_print_result(key, key_sum)

except "end of file":
return None

if __name__ == "__main__":
import sys
reduce_phase(sys.argv[1])

- ohad October 14, 2015 | 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