Google Interview Question for Java Developers


Country: United States




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

If want to cut stick of length 10 meters at 2, 4 and 7 meters from one end. Then it means that you need sticks of lengths 2, 2, 3, 3 meters, isn't it? Like if you make a first cut from an end at 2m you get stick of length 2m then again at 4m, you will get stick of length 2m and finally at 7m you get sticks of length 3m and remaining stick of 3m.

Is it correct?

- Anwar February 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StickPieces {

	class Result {
		int cost = Integer.MAX_VALUE;
		List<Integer> order = null;

		@Override
		public String toString() {
			return "Result [cost=" + cost + ", order=" + order + "]";
		}
	}

	public void findMinCost(int stickLength, List<Integer> indices) {
		Result result = new Result();
		for (int i = 0; i < indices.size(); i++) {
			recurse(stickLength, indices, i, new boolean[indices.size()],
					new ArrayList<Integer>(), result);
		}
		System.out.println(result);
	}

	private void recurse(int stickLength, List<Integer> indices, int curIndex,
			boolean used[], List<Integer> curOrder, Result result) {
		used[curIndex] = true;
		curOrder.add(indices.get(curIndex));
		if (curOrder.size() == indices.size()) {
			int cost = getCost(stickLength, curOrder);
			System.out.println("cost=" + cost);
			if (cost < result.cost) {
				result.cost = cost;
				result.order = new ArrayList<Integer>(curOrder);
			}
		} else {
			for (int i = 0; i < indices.size(); i++) {
				if (used[i] == false) {
					recurse(stickLength, indices, i, used, curOrder, result);
				}
			}
		}
		used[curIndex] = false;
		curOrder.remove(curOrder.size() - 1);
	}

	int getCost(int stickLength, List<Integer> order) {
		int cost = stickLength;
		List<Integer> pieces = new ArrayList<Integer>();
		for (int index : order) {
			if (pieces.isEmpty()) {
				pieces.add(index);
				pieces.add(stickLength - index);
			} else {
				int lenSoFar = 0;
				for (int pIdx = 0; pIdx < pieces.size(); pIdx++) {
					lenSoFar += pieces.get(pIdx);
					if (lenSoFar > index) {
						int size = pieces.remove(pIdx);
						cost += size;
						pieces.add(pIdx, size - (lenSoFar - index));
						pieces.add(pIdx + 1, lenSoFar - index);
						break;
					}
				}
			}
		}
		return cost;
	}

	public static void main(String args[]) {
		StickPieces sp = new StickPieces();

		sp.findMinCost(10, Arrays.asList(new Integer[] { 2, 4, 7 }));
		// 10 (4,6) + 4(2,2) + 6(3,3)

		sp.findMinCost(20, Arrays.asList(new Integer[] { 2, 4, 7, 12 }));
		// 20 (12,8) + 12(7,5) + 7(4,3) + 4(2,2)
	}
}

- Viv February 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StickPieces {

	class Result {
		int cost = Integer.MAX_VALUE;
		List<Integer> order = null;

		@Override
		public String toString() {
			return "Result [cost=" + cost + ", order=" + order + "]";
		}
	}

	public void findMinCost(int stickLength, List<Integer> indices) {
		Result result = new Result();
		for (int i = 0; i < indices.size(); i++) {
			recurse(stickLength, indices, i, new boolean[indices.size()],
					new ArrayList<Integer>(), result);
		}
		System.out.println(result);
	}

	private void recurse(int stickLength, List<Integer> indices, int curIndex,
			boolean used[], List<Integer> curOrder, Result result) {
		used[curIndex] = true;
		curOrder.add(indices.get(curIndex));
		if (curOrder.size() == indices.size()) {
			int cost = getCost(stickLength, curOrder);
			System.out.println("cost=" + cost);
			if (cost < result.cost) {
				result.cost = cost;
				result.order = new ArrayList<Integer>(curOrder);
			}
		} else {
			for (int i = 0; i < indices.size(); i++) {
				if (used[i] == false) {
					recurse(stickLength, indices, i, used, curOrder, result);
				}
			}
		}
		used[curIndex] = false;
		curOrder.remove(curOrder.size() - 1);
	}

	int getCost(int stickLength, List<Integer> order) {
		int cost = stickLength;
		List<Integer> pieces = new ArrayList<Integer>();
		for (int index : order) {
			if (pieces.isEmpty()) {
				pieces.add(index);
				pieces.add(stickLength - index);
			} else {
				int lenSoFar = 0;
				for (int pIdx = 0; pIdx < pieces.size(); pIdx++) {
					lenSoFar += pieces.get(pIdx);
					if (lenSoFar > index) {
						int size = pieces.remove(pIdx);
						cost += size;
						pieces.add(pIdx, size - (lenSoFar - index));
						pieces.add(pIdx + 1, lenSoFar - index);
						break;
					}
				}
			}
		}
		return cost;
	}

	public static void main(String args[]) {
		StickPieces sp = new StickPieces();

		sp.findMinCost(10, Arrays.asList(new Integer[] { 2, 4, 7 }));
		// 10 (4,6) + 4(2,2) + 6(3,3)

		sp.findMinCost(20, Arrays.asList(new Integer[] { 2, 4, 7, 12 }));
		// 20 (12,8) + 12(7,5) + 7(4,3) + 4(2,2)
	}
}

- Anonymous February 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class StickPieces {

	class Result {
		int cost = Integer.MAX_VALUE;
		List<Integer> order = null;

		@Override
		public String toString() {
			return "Result [cost=" + cost + ", order=" + order + "]";
		}
	}

	public void findMinCost(int stickLength, List<Integer> indices) {
		Result result = new Result();
		for (int i = 0; i < indices.size(); i++) {
			recurse(stickLength, indices, i, new boolean[indices.size()],
					new ArrayList<Integer>(), result);
		}
		System.out.println(result);
	}

	private void recurse(int stickLength, List<Integer> indices, int curIndex,
			boolean used[], List<Integer> curOrder, Result result) {
		used[curIndex] = true;
		curOrder.add(indices.get(curIndex));
		if (curOrder.size() == indices.size()) {
			int cost = getCost(stickLength, curOrder);
			System.out.println("cost=" + cost);
			if (cost < result.cost) {
				result.cost = cost;
				result.order = new ArrayList<Integer>(curOrder);
			}
		} else {
			for (int i = 0; i < indices.size(); i++) {
				if (used[i] == false) {
					recurse(stickLength, indices, i, used, curOrder, result);
				}
			}
		}
		used[curIndex] = false;
		curOrder.remove(curOrder.size() - 1);
	}

	int getCost(int stickLength, List<Integer> order) {
		int cost = stickLength;
		List<Integer> pieces = new ArrayList<Integer>();
		for (int index : order) {
			if (pieces.isEmpty()) {
				pieces.add(index);
				pieces.add(stickLength - index);
			} else {
				int lenSoFar = 0;
				for (int pIdx = 0; pIdx < pieces.size(); pIdx++) {
					lenSoFar += pieces.get(pIdx);
					if (lenSoFar > index) {
						int size = pieces.remove(pIdx);
						cost += size;
						pieces.add(pIdx, size - (lenSoFar - index));
						pieces.add(pIdx + 1, lenSoFar - index);
						break;
					}
				}
			}
		}
		return cost;
	}

	public static void main(String args[]) {
		StickPieces sp = new StickPieces();

		sp.findMinCost(10, Arrays.asList(new Integer[] { 2, 4, 7 }));
		// 10 (4,6) + 4(2,2) + 6(3,3)
	}
}

- Anonymous February 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is The ACM contest problem. Has it been really asked at a Google interview?

- bbdev March 04, 2018 | 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