Google Interview Question


Country: United States




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

I assume the sequence in which they move is not strictily alternating. Otehrwise the
problem would be a bit primitive. E.g. best might be left, left, left = 3 moves for
{1,2,3,1,2,3,1,2,3}

so, it really depends from which side we approach e.g.
{1,2,3,1,2,3,10,9,8,7,6,5,4,3,2,1,1,2,3,1,2,3}
is solved by left, left, right, right, right, right, right, right, right

it seems best, to approach it from left and right side and figure where to stop
so that min moves from left and right are needed.

I create two arrays with moves only from left and only from right and build the
min sum of the two arrays A1, A2 as min(A1[i]+A2[i]) for all 0 <= i < n

This would be an O(n) time and O(n) space approach.

#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int calcMinMoves(const vector<int>& skyline)
{
	size_t n = skyline.size();
	
	// from left
	vector<unsigned int> costL(n, 1);
	for (size_t i = 1; i < n; ++i) {
		if (skyline[i - 1] <= skyline[i]) {
			costL[i] = costL[i - 1];
		} else {
			costL[i] = costL[i - 1] + 1;
		}
	}

	// from right
	// note: from left and from right should be placed in an auxiliry 
	//   function, but for better visibility of the actual algo, I left it 
	//   as redundant code
	//   the aux-func had the following signature:
	//   vector<unsigned int> calcMovesFromOneSide(skyline, int start, int direction)
	vector<unsigned int> costR(n, 1);
	for (size_t i = n - 2; i < n; --i) {
		if (skyline[i + 1] <= skyline[i]) {
			costR[i] = costR[i + 1];
		} else {
			costR[i] = costR[i + 1] + 1;
		}
	}
	
	// find min moves
	costR[n - 1] = 0; // special case when only from left (last on right, could be from left or right)
	costL[0] = 0; // special case when only from right 
	unsigned int minMoves = n;
	for (size_t i = 0; i < n; i++) {
		minMoves = min(minMoves, costR[i] + costL[i]);
	}
	return minMoves;
}


int main()
{
	cout << (calcMinMoves({1,2,3,1,2,3,1,2,3}) == 3) << endl; 
	cout << (calcMinMoves({ 3,2,1,3,2,1,3,2,1 }) == 3) << endl;
	cout << (calcMinMoves({ 1,2,3,4,5,6,7,8,9 }) == 1) << endl;
	cout << (calcMinMoves({ 1,2,3,4,5,4,3,2,1 }) == 2) << endl;
	cout << (calcMinMoves({ 1,2,3,4,5,5,5,4,3,2,1 }) == 2) << endl;
	cout << (calcMinMoves({ 1,1,1,1,1,1 }) == 1) << endl;
	cout << (calcMinMoves({ 1,2,2,2,2,2 }) == 1) << endl;
	cout << (calcMinMoves({ 2,2,2,2,2,1 }) == 1) << endl;
	cout << (calcMinMoves({ 1,2,3,1,2,3,9,8,7,6,5,4,3,2,1,1,2,3,1,2,3 }) == 7) << endl;
}

- Chris September 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * Complete the function below.
 */

 static int getMinimumMoves(int[] height) {
                int[][] dp = new int[height.length][height.length];
		        return getMinimumNumberOfMoves(0,height.length-1, height,dp);
		    }

		    static int getMinimumNumberOfMoves(int start, int end, int[] a,int[][] dp){
		        
                
		        if(start>end) return 0;
		        if(start == end) return 1;
                if(dp[start][end] != 0) return dp[start][end];
		        
		        int count = 0;
		        
		        int i=start;
		        while(i<end && a[i]<=a[i+1])
		        		i++;
		        
		        int j = end;
		        while(j>start && a[j]<=a[j-1])
		            j--;
		        
		        int count1 = 1+ getMinimumNumberOfMoves(i+1, end, a,dp);
		        int count2 = 1+ getMinimumNumberOfMoves(start,j-1, a,dp);
		        
		        
//		        System.out.println(start + " "+ end +" " +count1 + " " + count2);
                dp[start][end] = Math.min(count1, count2);
	            return dp[start][end];
		        
		    }

- new September 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
   int[] buildings = {1, 2, 1, 2, 10, 9};
   System.out.println(calcMoves(buildings));
 }
  
  //1, 2, 3, 4, 8, 7, 6, 5
  //1, 2, 1, 2, 10, 9
  public static int calcMoves(int[] buildings){
  	
    int j = buildings.length -1;
    int i = 0;
    
    int moves = 0;
    while(i < j){
    
      int iI = i;
      int jI = j;
      
      while(i+1 <= j && buildings[i] < buildings[i+1]){
      	i++;
      }
      if(i > iI)
        moves++;
      
      while(j-1 >= i && buildings[j] < buildings[j-1]){
      	j--;
      }
      if(j < jI)
        moves++;
      
      i++;
      j--;
    }
    return moves;
  }

- sudip.innovates September 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@stupid.innovates
[1,1,1,1,1] -> can you handle the case where the building height are equal.


Nice solution

- new September 22, 2017 | 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