Zynga Interview Question


Country: United States
Interview Type: In-Person




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

This is an easy question.
this link
onestopinterviewprep.blogspot.com/2014/03/max-apples-that-can-be-collected-in-2d.html
has a similar problem.

- Tormentor March 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

You are the guy who keeps spamming the chats. Please stop. Nobody cares about your onestopinterviewprep (e.g., the next careercup killer).

- Mike Davis March 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I guess the qn should be moving right or down nor right or left ??

- arsragavan March 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess it should be right or down, we cannot solve with left or right.

- kirankumarcelestial March 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple Dp problem.

- icodingc March 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As you can move right or <b>down</b> only so a cell say (i,j) can be reached from (i-1,j) or (i,j-1) i.e from its left or from its upper cell only what we'll do is, to every cell (i,j) we will add the max[(i,j-1) AND (i-1),j] this will make the value at every (i,j) cell as maximum keeping in sync with the fact that only right and downward transitions are allowed.

public class MaxEggCollectionProblem {
	public static void main(String[] args) {
		int[][] eggsMapping = new int[][] { { 2, 4, 8 }, { 5, 6, 3 },
				{ 4, 7, 6 } };

		int numberOfRows = eggsMapping.length;
		int numberofColumns = eggsMapping[0].length;

		printArray(eggsMapping, numberOfRows, numberofColumns);

		// As you can move right or down only so a cell say (i,j) can be reached
		// from (i-1,j) or (i,j-1) i.e from its left or from its upper cell only
		// what we'll do is, to every cell (i,j) we will add the max[(i,j-1) AND
		// (i-1),j]
		// this will make the value at every (i,j) cell as maximum keeping in
		// sync with the fact that only right and downward transitions are
		// allowed

		findMaxEggs(eggsMapping, numberOfRows, numberofColumns);
		
		System.out.println("\n\n");
		System.out.println("Max eggs collected at cell 1,2 is " + eggsMapping[1][2]);
		System.out.println("Max eggs collected at cell 2,2 is " + eggsMapping[2][2]);

	}

	private static void printArray(int[][] eggsMapping, int numberOfRows,
			int numberofColumns) {
		System.out.println("===================");

		// Print the array
		for (int i = 0; i < numberOfRows; i++) {
			for (int j = 0; j < numberofColumns; j++) {
				System.out.print(eggsMapping[i][j] + "\t");
			}
			System.out.println("");
		}

	}

	/**
	 * As you can move right or down only so a cell say (i,j) can be reached
	 * from (i-1,j) or (i,j-1) i.e from its left or from its upper cell only
	 * what we'll do is, to every cell (i,j) we will add the max[(i,j-1) AND
	 * (i-1),j] this will make the value at every (i,j) cell as maximum keeping
	 * in sync with the fact that only right and downward transitions are
	 * allowed
	 * 
	 * @param eggsMapping
	 * @param numberofColumns
	 * @param numberOfRows
	 */
	private static void findMaxEggs(int[][] eggsMapping, int numberOfRows,
			int numberofColumns) {
		for (int i = 0; i < numberOfRows; i++) {
			for (int j = 0; j < numberofColumns; j++) {

				int leftWeight = -1;
				int topWeight = -1;

				leftWeight = (i - 1) < 0 ? 0 : eggsMapping[i - 1][j]; 
				topWeight = (j - 1) < 0 ? 0 : eggsMapping[i][j-1];
				
				eggsMapping[i][j] += Math.max(leftWeight, topWeight);
			}
		}

		printArray(eggsMapping, numberOfRows, numberofColumns);
	}
}

- SumitGaur April 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

leftWeight should be (j-1 ) instead of (i-1) . Though the final answer won't change.

- techdebugger.zg June 01, 2015 | Flag


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