Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Above approach looks fine .. but there is problem with cell object inheriting from ant,food and water ... if ant enters into a food cell than that cell should become an ant cell ... but we can't do this since the food cell is already inherting from food cell and we can't make it inherit from ant cell dynamically ...
solution would be to create a cell class and each cell would have a state/strategy names ant , food water and depending upon the condition we can dynamically chage the state of cell. This state object will handle all the scenarios for that particular cell type.
class antGame has following member variables
1>movement is a enum or macro which have 4 possible value left,right,up and down;and nextCell is the type of cell like food, water and ant;
2> method move(string movement); Where the user can make the ant move to the cell specified by the movement value of nextcell as parameter.
3> method nextCell(string Cell,string movementl); will accept Cell as the current cell and next cell as the movement parameter and returns the output of the movement.
4> method nextCellContains(string Cell,string movement); accepts the same parameters but returns what is the content of the adjacent cell.
5> has a class Object ant(may be an independent class), which needs to be created by constructor and destroyed not only by destructor but also the dissolve movement.
6> Object ant has to be singleton and is accessible to two different antGame object.
The list will go on and on.. after discussion.. But really good question..
You need to figure out what objects would be relevant.
- Frank March 02, 2013Off the bat I see:
-Cell object which could be extended to Ant, Food,Water.
-Board Object, which consists of cells
-Player object, which are the attributes both players have when playing this game.
-Game Manager, essentially holds all the rules and validates moves
So the Cell essentially just holds what type of cell it is. Nothing more nothing less.
The Board class could be set up as matrix and inside each index of the matrix is a Cell object that represents the appropriate cell. You'd also need methods such as move cell, which would move the appropriate cell to specified location.
This brings me to the next object, the Game Manager. The Game Manager would look at whether or not the moves are legal. If they're illegal then it'd notifhy the board object. If legal then it'd notify the board object. Things like that...
The Player class would have a constructer which assigns a username to each player and maybe a uniquieID. You'd have methods like move, skip, send message, etc...
The list truly does go on for a long while. The biggest thing here is that you need some basics, and then once you get the basics, you can add even more functionality.