Interview Question


Team: Java
Country: India
Interview Type: Written Test




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

Java code //not fully implemented. it is already 100 lines!!

interface data{
	//cell types;
	enum cellType={EMPTY=0,MINE=1,LIFE=2,MONEY=3,CLICKED=4};
	const int SIZE=8;

} 
class Board implements data
{
	int[][] board;
	int noOfMines;
	int size;
	Board()
	{
		board=new int[size][zise];
		size=SIZE;
		initCells();
	}
	Board(int n)
	{
		size=n;
		board=new int[size][zise];
		initCells();
	}
	void initCells()
	{
		Random rand=new Random();
		noOfMines=7+rand.nextInt(7);//7 to 13 mines per game
		plantMines();//not implemented.just use a random fn to get i and j and put board[i][j]=MINE
	}
	int move(int x)//returns score added
	{
		int i=x/8,j=x%8;//get cell location x= i*8 + j
		int val=board[i][j];
		if(val==CLICKED)
			return 0;//already clicked . do nothing
		if(val==EMPTY)
		{
			int scoreToAdd=openUpCellsRecursively();
			return scoreToAdd;
		}
		else if(val==MINE)
		{
			alive=false;
			openSingleCell(x);
			return -1;//-1=dead
		}
		else if(val==MONEY)
		{
			return 100;//100 score bonus on clicking a MONEY cell 
		}
	}
}
class Player{
	String name;
	int noOflives;
	boolean alive,won;
	void init()
	{
		alive=true;
		won=false;
		noOflives=1;
	}
	Player()
	{
		init();
		name="player";
	}
	Player(String pname)
	{
		init();
		name=pname;
	}	
	int getinput()
	{
		Scanner sc=new Scanner(System.in);
		int x=sc.nextInt();//input cell no, assuming cells are numbered 1 to 64 row-wise for size=8
		return x;
	}
}
class Game
{
	Player player;
	Board board;
	boolean alive;
	public static void main()
	{
		player=new Player("John");
		board=new Board();
		while(player.alive && !player.won)
		{
			int input=player.getinput();
			int val=board.move(input);
			if(val==-1)
				player.alive=false;
			else
				if(val>0)
					player.score+=val;
		}
	}
}

- neerajlakhotia08 May 10, 2014 | Flag Reply


Add a Comment
Name:

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

Books

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

Videos

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