Graphic Systems Interview Question
Senior Software Development EngineersCountry: United States
We move left and right, marking position at constant step (stepSize). Checking the presence of no own markers. If found, relax and wait other sim.
public void collide() {
found = false;
ownMarkers = 1;
stepSize = 10;
step = 0;
countedMarkers = 1;
direction = 0;
this->MarkPosition();
while(!found)
{
# Moving step in direction
if(direction == 0)
{
this->MoveLeft();
}
else
{
this->MoveRight();
}
step++;
# Marked Position?
PositionMarked = this->IsCurrentPositionMarked();
# Own Marker or other sim found?
if(PositionMarked == True)
{
if((step==stepSize) && (countedMarkers < ownMarkers))
{
# Found own marker, continue to next own marker (other stepSize moves) in same direction
countedMarkers++;
step = 0;
}
else
{
found = true;
}
}
else
{
if(step==stepSize)
{
# Other direction
this->MarkPosition();
ownMarkers++;
countedMarkers = 1;
step = 0;
direction = !direction;
}
}
}
while(true)
{
this->Relax();
}
We move left and right, marking position at constant step (stepSize). Checking the presence of no own markers. If found, relax and wait other sim.
public void collide() {
found = false;
ownMarkers = 1;
stepSize = 10;
step = 0;
countedMarkers = 1;
direction = 0;
this->MarkPosition();
while(!found)
{
# Moving step in direction
if(direction == 0)
{
this->MoveLeft();
}
else
{
this->MoveRight();
}
step++;
# Marked Position?
PositionMarked = this->IsCurrentPositionMarked();
# Own Marker or other sim found?
if(PositionMarked == True)
{
if((step==stepSize) && (countedMarkers < ownMarkers))
{
# Found own marker, continue to next own marker (other stepSize moves) in same direction
countedMarkers++;
step = 0;
}
else
{
found = true;
}
}
else
{
if(step==stepSize)
{
# Other direction
this->MarkPosition();
ownMarkers++;
countedMarkers = 1;
step = 0;
direction = !direction;
}
}
}
while(true)
{
this->Relax();
}
Given a current sim location, I would radially start exploring the presence of another sim. This means first looking at radius increments of 1,2,3, etc... (i.e look 1unit right / 1 unit left, then 2 units right / 2 units left, etc..) until another sim is found. Once we know the direction in which the other sim is found, we can start moving in that direction by calling moveLeft() or moveRight(). This way, eventually, both sims will collide. I have outlined a simple solution in Python below:
Solution:
def collide():
# Mark current location
currSim.markPosition()
# Radially search for another marked location
# (this indicates the presence of another sim)
isRight = True
increment = 1
while True:
rightLocation = currentLocation + increment
leftLocation = currentLocation - increment
if isCurrentLocationMarked(rightLocation):
break
elif isCurrentLocationMarked(leftLocation):
isRight = False
break
increment += 1
# If another sim is towards right
# move right
if isRight:
currSim.moveRight()
currSim.markPosition()
else: # else move left
currSim.moveLeft()
currSim.markPosition()
At the beginning make each sim mark it's position and have them both move in the same direction(Right or Left it doesn't matter), with each coming step make each sim checks whether the current position is marked or not (if not mark it and continue moving in the same direction), since both sims are moving in the same direction the case that a sim steps on a marked position will only happen when one sim steps over the other sim's original position, after that all we have to do is to make the current sim move twice as fast as the other one and they will definitely collide at some point.
Code:
- sbdul March 22, 2018