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
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);
}
}

- mayankjaiho8 May 30, 2018 | Flag Reply
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);
  }
}

- mayankjaiho8 May 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
	int length;
	cin>>length;
	int n;
	cin>>n;
	int a[n+2];
	a[0] = 0;
	for(int i=1; i<=n; i++)
	{
		cin>>a[i];
	}
	a[n+1] = length;
	int dp[n+2][n+2];
	for(int i=0; i<=n; i++)
	{
		dp[i][i+1] = 0;
	}
	for(int l=2; l<=length; l++)
	{
		for(int i=0; i<n+1-l+1; i++)
		{
			int j = i+l;
			int min = INT_MAX;
			for(int k = i+1; k<j; k++)
			{
				int x = dp[i][k] + dp[k][j] + a[j] - a[i];
				if(x < min)
				{
					min = x;
				}
			}
			dp[i][j] = min;
		}
	}
	cout<<dp[0][n+1];
}

- Nishagrawa March 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursion with memoization.
O(N^2) time, O(N^2) space complexity (N is number of cuts)

#include <vector>
#include <limits.h>

int minPriceWoodCutting(int wood_start, int wood_end, const std::vector<int>& cuts, int cuts_start, int cuts_end, std::vector<std::vector<int>>& memo){
  if(cuts_start > cuts_end || wood_end == wood_start){
    return 0;
  }

  if(memo[cuts_start][cuts_end] != -1){
    return memo[cuts_start][cuts_end];
  }

  int wood_length = wood_end - wood_start;

  int min_price = INT_MAX;
  for(int i = cuts_start;i<=cuts_end;++i){
    int price_left = minPriceWoodCutting(wood_start, cuts[i], cuts, cuts_start, i - 1, memo);
    int price_right = minPriceWoodCutting(cuts[i], wood_end, cuts, i + 1, cuts_end, memo);

    if(price_left + price_right < min_price){
      min_price = price_left + price_right;
    }
  }

  int total_price = wood_length + min_price;

  memo[cuts_start][cuts_end] = total_price;
  return total_price;
}

int minPriceWoodCutting(int length, const std::vector<int>& cuts){
  std::vector<std::vector<int>> memo;
  memo.resize(cuts.size());
  for(int i=0;i<cuts.size();++i){
    memo[i].resize(cuts.size(), -1);
  }

  return minPriceWoodCutting(0, length, cuts, 0, cuts.size()-1, memo);
}

int main()
{
  std::vector<int> cuts = {2,4,7};
  int length = 10;
  int min_price = minPriceWoodCutting(length, cuts);

  std::cout<<"Min price: "<<min_price<<std::endl;

  return 0;
}

- tskrgulja1 March 19, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

l

- Anonymous March 20, 2019 | 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