Backend Engineer Interview Question

suraj j unni
2 min readAug 15, 2021

Here’s one of the questions asked for Backend Engineer Role by FAANG Companies.

Design a backend API for a two player game.

Here player were given two choices:1)Dove 2)Eagle

In Dove strategy, the player will always try to compromise with other player thereby scoring two each.

In Eagle strategy, the player will always try to cheat other player thereby scoring three and losing one point .

Here is the link of Actual game:https://ncase.me/trust/

Apart from these two strategies, we need to create custom strategy for the game.

Let me come to solution part.

First of all, we need to figure out the attributes to be considered for the game that could be stored in the database.

Here I have created two models:

  1. Player_Info: This tells about the user. All the data will be entered when user registers for the game.
  2. Game_Score: Here I am storing the current score, iteration, strategy used by the player.

Lets Come to backend API apart.

I have created 3 strategies here:

  1. Dove strategy:

If both players choose Dove then both will be scoring 2 each else one will score 3 other will lose one point.

def DoveStrategy(data,user_score=0,other_score=0):
if(data[‘user_side’]==”Eagle”):
user_score+=3
other_score-=1
if(data[‘user_side’]==”Dove”):
user_score+=2
other_score+=2
resp=write_to_database(data,user_score,other_score)
return resp

2.Eagle Strategy

If both players choose Eagle then no points are scored by both players.

def EagleStrategy(data,user_score=0,other_score=0):
if(data[‘user_side’]==”Dove”):
user_score-=1
other_score+=3
resp=write_to_database(data,user_score,other_score)
return resp

3.Optimum Strategy

The other player will choose the option taken by the current player on his previous iteration.

if request.method == ‘POST’ and request.data[“stratergy”]==”optimum”:
mail = request.data.get(‘email’)
#print(mail)
per_type=Player.objects.get(email=mail)
a=Game.objects.filter(email=per_type).first()
if(request.data[‘user_side’]==”Dove” and a.side==”Dove”):
return DoveStratergy(request.data)
elif(request.data[‘user_side’]==”Dove” and a.side==”Eagle”):
return DoveStratergy(request.data)
else:
return EagleStratergy(request.data)

I will be updating the code in my GitHub profile later.

Follow my blog for reading about interviews related to various role.

--

--