Interview Question
Site Reliability Engineer Interview
-
Booking.comThey gave me the below question to solve in 30 mins. Based on customer research, we know that our guests get confused when they are searching for accommodation and they found multiple hotels with the same name in the same city. To avoid this, we want to create a tool to identify "confusing" cities: cities with at least 3 hotels with the same name. Given a list of tuples (hotel_id, hotel_name, city) return a list of all "confusing" cities. Input: [ {hotel_1234, "Sheraton", "Amsterdam"} , {hotel_1000, "Sheraton", "Buenos Aires"} , {hotel_1001, "Hilton", "Amsterdam"} , {hotel_1002, "Royal Palace", "Bogota"} , {hotel_1003, "Hilton", "Amsterdam"} , {hotel_1004, "Sheraton", "Buenos Aires"} , {hotel_1005, "Sheraton", "Buenos Aires"} ] Output: [ "Buenos Aires" ]
Interview Answers
8 Answers
Another approach would be to create a hashtable/dict with key as a tuple (city,hotel name) and value to be the occurrence of the hotel name in that city (count, keep incrementing the count when seen). When the hashtable is created, iterate over items and see whose value >= 3 and return the tuple's/key 1st value
Anonymous on
using golang ---- package main import ( "fmt" ) func main() { input := [][]string{{"hotel_1234", "Sheraton", "Amsterdam"}, {"hotel_1000", "Sheraton", "Buenos Aires"}, {"hotel_1001", "Hilton", "Amsterdam"}, {"hotel_1002", "Royal Palace", "Bogota"}, {"hotel_1003", "Hilton", "Amsterdam"}, {"hotel_1004", "Sheraton", "Buenos Aires"}, {"hotel_1005", "Sheraton", "Buenos Aires"}, } confuse_cities := []string{} Cities := make(map[string]map[string]int) for _, line := range input { if _, ok := Cities[line[2]]; ok { if val, ok2 := Cities[line[2]][line[1]]; ok2 { Cities[line[2]][line[1]] = val + 1 if Cities[line[2]][line[1]] == 3 { confuse_cities = append(confuse_cities, line[2]) } } } else { Cities[line[2]] = map[string]int{} Cities[line[2]][line[1]] = 1 } } fmt.Println(confuse_cities) }
Anonymous on
def confuse(): hotels = [("hotel_1234", "Sheraton", "Amsterdam") ,("hotel_1000", "Sheraton", "Buenos Aires") ,("hotel_1001", "Hilton", "Amsterdam") ,("hotel_1002", "Royal Palace", "Bogota") ,("hotel_1003", "Hilton", "Amsterdam") ,("hotel_1004", "Sheraton", "Buenos Aires") ,("hotel_1005", "Sheraton", "Buenos Aires"),("hotel_1234", "Sheraton", "Amsterdam")] from collections import defaultdict d = defaultdict(int) for hotel in hotels: d[(hotel[1], hotel[2])] += 1 for k, v in d.items(): if v >= 3: print(k)
Anonymous on
I answered the questions but took too much time to solve it after some help, I was nerveous too because I come from Ops background with no prior programming experience, but still managed to learn it from my own. Now I know what are my weak points! Please note that they only want your approach not the actual program, so all the best to your interview, hope it helps!
Anonymous on
Also possible solution: #/usr/bin/python3 import yaml if __name__ == '__main__': # file.txt contains Input data with open("file.txt") as f: res = yaml.load(f) only_uniq = set() for d in res["Input"]: only_uniq = list(d)[2] print(only_uniq)
Vlad on
Input = [ ("hotel_1234", "Sheraton", "Amsterdam") , ("hotel_1000", "Sheraton", "Buenos Aires") , ("hotel_1001", "Hilton", "Amsterdam") , ("hotel_1002", "Royal Palace", "Bogota") , ("hotel_1003", "Hilton", "Amsterdam") , ("hotel_1004", "Sheraton", "Buenos Aires") , ("hotel_1005", "Sheraton", "Buenos Aires"), ("hotel_1234", "Sheraton", "Amsterdam"), ] confusing_cities = [] for i in Input: counter = 0 if i[2] in confusing_cities: continue for k in Input: if i[1] == k[1] and i[2] == k[2]: counter +=1 if counter == 3: confusing_cities.append(i[2]) break print(confusing_cities)
Anonymous on
from collections import Counter Input = [ ("hotel_1234", "Sheraton", "Amsterdam") , ("hotel_1000", "Sheraton", "Buenos Aires") , ("hotel_1001", "Hilton", "Amsterdam") , ("hotel_1002", "Royal Palace", "Bogota") , ("hotel_1003", "Hilton", "Amsterdam") , ("hotel_1004", "Sheraton", "Buenos Aires") , ("hotel_1005", "Sheraton", "Buenos Aires"), ("hotel_1232", "Sheraton", "Amsterdam"), ("hotel_123222", "Sheraton", "Amsterdam") ] confusing_cities = [] for i,v in Counter((elem[1],elem[2]) for elem in Input).items(): if v >= 3: confusing_cities.append(i[1]) print(confusing_cities)
Anonymous on
item = Input[-1] s = set() for item in Input: if last_item[1] == item[1]: if last_item[2] == item[2]: s.add(item[2]) last_item = item print(s)
Anonymous on