Bloomberg | Video | New Grad SWE (London)

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2020-02-28Added By Anonymous
Source: Link
Bloomberg | Video | New Grad SWE (London)

Given a Subway system where the customer can swipe in his card to check in and swipe out his card to check out at destination station, get the average time needed to travel between any 2 stations. and I should save customer Id, create swipeIn and swipeOut functions and another to get the average

class Passenger:

def __init__(self):
self.tap_in_time = None

passengers = {}
time_increments = []


def tapIn( cardId, station, time ):
if cardId in passengers:
passengers[cardId].tap_in_time = (station, time)
else:
passengers[cardId] = Passenger()
passengers[cardId].tap_in_time = (station, time)


def tapOut( cardId, station, time ):
tap_station, tap_time = passengers[cardId].tap_in_time
time_increment = time-tap_time
time_increments.append((tap_station, station, time_increment))

def getAverage( startStation, endStation ):

prev_average = 0
prev_quantity = 0

for (tap_in_station, tap_out_station, times) in time_increments:

if tap_in_station == startStation and tap_out_station == endStation:

new_quantity = prev_quantity+1

new_average = ((prev_average * prev_quantity) + times)/new_quantitiy

prev_average = new_average
prev_quantity = new_quantitiy

return prev_average



def __main__:
tapIn( 4000, "A", t0)
tapIn( 5000, "C", t1 )
tapIn( 6000, "A", t2 )
tapOut( 4000, "D", t3)
tapOut( 5000, "E", t4 )
tapIn( 7000, "R", t5 )
tapOut( 6000, "X", t6 )
tapOut( 7000, "X", t7 )
..
..
..
..

getAverage( startStation, endStation )

tapOut( 4000, "D", t13)
tapOut( 5000, "E", t14 )
tapIn( 7000, "R", t15 )
tapOut( 6000, "X", t16 )
tapOut( 7000, "X", t17 )

getAverage( startStation, endStation )

Comments