Ebay Interview Question for SDE1s


Country: United States
Interview Type: In-Person




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

Represent the coordinates as a 2-dimensional array, where the value is 0 if the coordinate is empty and x if it's occupied by ship x. Also use a HashMap to keep track of each ship's health. Update both as we fire at each coordinate.

// positive integer represents a ship
// zero represents an empty space
// -1 represents it has been shot already
private int[][] coord;

// shipHealth.get(i) represents the initial area of ship i
// it gets decremented as the ship gets shot
private HashMap<Integer, Integer> shipHealth;

public Battleship(int[][] coord) {
	this.coord = coord;
	shipHealth = new HashMap<Integer, Integer>();
	for(int i=0; i<coord.length; i++) {
		for(int j=0; j<coord[0].length; j++) {
			int value = coord[i][j];
			if(value == 0)
				continue;
			if(!shipHealth.containsKey(value))
				shipHealth.put(value, 0);
			shipHealth.put(value, shipHealth.get(value) + 1);
		}
	}
}

public void shoot(int x, int y) {
	int value = coord[x][y];
	if(value == -1) {
		System.out.println("Already Attacked");
	} else if(value == 0) {
		System.out.println("Missed");
	} else {
		shipHealth.put(value, shipHealth.get(value) - 1);
		if(shipHealth.get(value) == 0)
			System.out.println("Ship sunk");
		else System.out.println("Attacked Ship " + value);
	}
	coord[x][y] = -1;
}

- Sunny June 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We dont need a HashMap to keep track of each ship's health

Instead of using x as a placeholder, use a number representing the health of the ship. Every time ship is attacked, decrease this number.

Once the number is -ve of zero, the ship sunk.

- yolo July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The reason I use a HashMap is because I don't know the #ships in advance and don't want to iterate through the whole map just to find out the #ships and hence the array size. Alternatively I can probably use an ArrayList, which grows dynamically.

- Sunny July 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you put a C code??

- Anonymous July 21, 2013 | 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