## 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?

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;
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()) {
} 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)
}
}``````

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;
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()) {
} 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)
}
}``````

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;
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()) {
} 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)
}
}``````

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?

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

// package whatever; // don't place package name!

import java.io.*;
import java.util.*;

class MyCode
{

private static int getMinCost(int[] arr, int length)
{
return getMinCost(arr, 0, length, 0, arr.length-1);
}

private static int getMinCost(int[] arr, int startLength, int endLength, int startIdx, int endIdx)
{
if(startIdx > endIdx)
return 0;

System.out.println("startIdx -> " + startIdx + " endIdx -> " + endIdx);
int currentCost = endLength - startLength;
int minCost = Integer.MAX_VALUE;

for(int i=startIdx; i <= endIdx; i++)
{
int currentMinCost = getMinCost(arr, startLength, arr[i], startIdx, i-1) +
getMinCost(arr, arr[i], endLength, i+1, endIdx);

if(currentMinCost < minCost)
minCost = currentMinCost;
}

return currentCost + minCost;
}

public static void main (String[] args)
{

int arr[] = new int[3];
arr[0] = 2;
arr[1] = 4;
arr[2] = 7;
int length = 10;

int minCost = getMinCost(arr, length);

System.out.println("Min Cost -> " + minCost);
}
}

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

``````// package whatever; // don't place package name!

import java.io.*;
import java.util.*;

class MyCode
{
private static class Pair
{
int start;
int end;

@Override
public boolean equals(Object o)
{
Pair p = (Pair)o;

return (start == p.start) && (end == p.end);
}

public int hashCode()
{
int prime = 101;

return prime * start + end ;
}
}

private static int getMinCost(int[] arr, int length)
{
HashMap<Pair,Integer> memoMap = new HashMap<Pair, Integer>();
return getMinCost(arr, 0, length, 0, arr.length-1, memoMap);
}

private static int getMinCost(int[] arr, int startLength, int endLength, int startIdx, int endIdx, HashMap<Pair,Integer> memoMap)
{
if(startIdx > endIdx)
return 0;

Pair pair = new Pair();
pair.start = startIdx;
pair.end = endIdx;

if(memoMap.containsKey(pair))
return memoMap.get(pair);

System.out.println("startIdx -> " + startIdx + " endIdx -> " + endIdx);

int currentCost = endLength - startLength;
int minCost = Integer.MAX_VALUE;

for(int i=startIdx; i <= endIdx; i++)
{
int currentMinCost = getMinCost(arr, startLength, arr[i], startIdx, i-1, memoMap) +
getMinCost(arr, arr[i], endLength, i+1, endIdx, memoMap);

if(currentMinCost < minCost)
minCost = currentMinCost;
}

memoMap.put(pair, currentCost + minCost);

return currentCost + minCost;
}

public static void main (String[] args)
{

int arr[] = new int[3];
arr[0] = 2;
arr[1] = 4;
arr[2] = 7;
int length = 10;

int minCost = getMinCost(arr, length);

System.out.println("Min Cost -> " + minCost);
}
}``````

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.

### 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.