## Google Interview Question

SDE1s**Country:**India

**Interview Type:**Written Test

In Python,

```
#!/usr/bin/python
#Global Vars
teams = 10
hf = 0.8
objTeamList = []
track = 10000 #10 Km
timer = 2
usedNitro = []
completed = []
#Class Def
class Team:
speed = 0
distance = 0
time = 0
top_speed = 0
hasNitro = 1
def __init__(self, index):
self.speed = 0
self.time=0
self.distance = 200 * (teams - index)
self.top_speed = 150 + 10 * index
self.accelaration = 2 * index
def __lt__(self, team1):
return self.distance < team1.distance
def __str__(self):
return "Time:[%s] Speed:[%s]" % (self.time, self.speed)
def useNitro(self):
if self.speed * 2 < self.top_speed:
self.speed = self.speed * 2
else:
self.speed = self.top_speed
def updateStats(self, timer):
self.distance += self.speed * timer + 0.5 * self.accelaration * timer ** 2
self.speed += self.accelaration * timer
self.speed *= hf
self.time += timer
def getDistance(self):
return self.distance
def GetMin(array):
index=0
min = array[index]
for i in range(len(array)):
if i in completed:
continue
elif(array[i] < min):
index = i
min = array[i]
return index
########## Code ###########
#Starting Point
for i in range(teams):
t = Team(i+1)
objTeamList.append(t)
time_elapsed =0
#Ready, Steady, Go!
while(1):
if(len(completed) == teams):
print "----------------------------Race Completed-------------------------\n"
for i in range(teams):
print "%s:" % (i+1), objTeamList[i]
print "\n-----------------------------------------------------------------\n"
exit(1)
time_elapsed += timer
#Update & Reassessment
for i in range(teams):
if i not in completed:
objTeamList[i].updateStats(timer)
#Nitro
last1 = GetMin(objTeamList)
if last1 not in usedNitro:
objTeamList[last1].useNitro()
usedNitro.append(last1)
#Check for Winner(s)
for i in range(teams):
if i not in completed:
if objTeamList[i].getDistance() >= track:
completed.append(i)
```

This would be a simple iterative simulation...

}

- zortlord May 20, 2015