Amazon Interview Question for SDE-2s

Team: no
Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
of 1 vote

You can long poll but for interview a better answer would be to use sockets such that you create a connection from client to server, and now server can push notification to you when events happens. This in contrast to long polling is less stressful on client as it does not use up as much resource polling.

Its hard to talk about the design here, but I'll give a general overview of how one might go about implementing this at a general level:

- Visual Layer: 
  - GameModel: Current GameModel fetched from api (just some json data model)
  - GameRender: Given a gameModel will render the chess game
  - Create connection with api
     - onUpdate: update current gameModel and re-render game

- Api Layer
  - /get/:create
     - Creates a brand new game model and returns the game model

  - /get/gameModel/:gameId
    - Given game id will query database to get the latest gameModel

  - /get/games/:playerId
    - Given player id, will return array of gameId that playerId has created

  - /post/update/
    - Will update the game with the latest game model. Latest game model is stored in the body of the post request

- Persistance Layer
  - Can choose between sql or nosql. For simplicity, lets pick mongodb which allows us to store 
    json models. This data we store can be optimized and will effect how efficient our request per second is in API layer

#General Userflow
1) User creates a opens up app
 - App checks if user has any game played via /get/games/playerId
 - App shows all games played if any

2) User can either create new game, or click on one of the played game which has a gameId
 - If clicking on a played game, we have the game id so call /get/gameModel:gameId
  - get the game model and render it
 - If creating new game, call /get/create, which returns a new gameModel
  - get the game model and render it

3) Once game model is rendered wait for user to make move
  - Store the move in the game model
  - call /post/:gameId with the current gameId and its gameModel in body of the request

4) Backend accepts the /post/update request
  - Updates database with the new gameModel for the given gameId  
  - Updates database with { gameId: gameId, lastPlayer: gameMode.playerId }
  - For gameId get all players in the game from Database (we may have {gameId: [array of playerid]} in db)
  - Now we have gameModel.playerId and allPlayerId, with that we can get the next playerId
  - Check socket if next playerId is connected, if so, send update gameModel
  - If not then when next playerId connects, it will be their turn

There are few details I haven't talked about, like what exactly does the gameModel look like?
Here is one way it could look :

 playerId: String #a unique player id
 gameId: String #a unique game id 
 historyOfMoves: Array #Array of moves which we can use to render the chessGame

There is obviously more to this, but thats a general overview. As for scaling, the default answer is to create multiple clusters of API which is gated by a load balancer. So when client request api endpoint, the load balancer intercepts the request, and delegates to a proper server which handles our restful request. And if we need to scale we can simply increase the number of clusters we have to handle more request.

I hope that was helpful to start.

- tnutty2k8 September 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

can somebody give me a link to read about such web based designs

- mohapatrasandeep60 September 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote is a good source.

Http question: use long polling from the client, so he can get notifications
Scalability: I would not necessarily say it's not scalable, there is not enough info given here to judge that. But the storage of pants in fields in classical relational manner is a bit hard to scale and unnecessary. You could just store a object as whole which contains the two set of pans with coordinates or the field as a whole. This can then be easily cached as a whole. That's easier to scale.
The password: it's bad practice to store it with encryption, store the hash and a seed, LS :-) is fun to read, then search for Google's Jeff Dean videos on YouTube, which I think is inspiring.

- ChrisK September 14, 2017 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More