Google Interview Question for SDE1s


Country: United States




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

So easy

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

So easy

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

So easy

println("So easy");

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

/* package whatever; // don't place package name! */
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		int ar[][] = {	{1,2,3,1},
						{0,1,4,2},
						{1,9,10,4}
					};
					maxArea(ar,11);
	}
	public static void maxArea(int ar[][],int limit){
		int costs[][] = new int[ar.length][ar[0].length];
 
		for(int i=0;i<ar.length;i++){
			for(int j=0;j<ar[0].length;j++){
				int cost=0;
				if(i>0){
					cost = cost + costs[i-1][j];
				}
				if(j>0){
					cost= cost +costs[i][j-1];
				}
				if(i>0 && j >0){
					cost = cost-costs[i-1][j-1];
				}
				costs[i][j]=cost+ar[i][j];
			}
		}
	//	System.out.println(Arrays.deepToString(costs));
		int p=0,q=0,r=0,s=0;
		int maxArea = 0;
		for(int i=-1;i<ar.length;i++){
			for(int j=-1;j<ar[0].length;j++){
				for(int k=i+1;k<ar.length;k++){
					for(int l=j+1;l<ar[0].length;l++){
						int cost=costs[k][l];
						if(i > 0){
							cost = cost-costs[i][l];	
						}
						if(j > 0 ){
							cost = cost-costs[k][j];
						}
						if( i > 0 && j >0){
							cost = cost + costs[i][j];	
						}
						if(cost <= limit){
							int area = (k-i) * (l-j);
							if(maxArea < area){
								p=i;
								q=j;
								r=k;
								s=l;
								maxArea=area;
							}
						}
 
					}
				}
			}
		}
	//	System.out.println(p+" "+q+" "+r+" "+s);
		for(int i=p+1;i<=r;i++){
			for(int j=q+1;j<=s;j++){
				System.out.print(ar[i][j] + " " );
			}
			System.out.println();
		}
	}

}

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

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;

#define DEBUG

int **arr, **arr1, R, C;
int maxLandVal = 11;
int maxBlock = 0;

void move(int r, int c, int landVal, int numBlocks, string s)
{
if (arr1[r][c] != 0 && arr1[r][c] <= numBlocks)
return;

++numBlocks;
landVal = landVal + arr[r][c];
arr1[r][c] = numBlocks;

if (landVal > 11 )
return;

if (landVal == maxLandVal && maxBlock < numBlocks)
{
maxBlock = numBlocks;
cout << "land value: " << landVal << endl;
cout << s << endl;
}



if (r < R - 1) move(r + 1, c, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
if (r > 0) move(r - 1, c, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
if (c < C - 1) move(r, c + 1, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
if (c > 0) move(r, c - 1, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");

arr1[r][c] = 0;
}


int main()
{
freopen("Text.txt", "r", stdin);
setbuf(stdout, NULL);
cin >> R;
cin >> C;
arr = new int*[R];
arr1 = new int*[R];

for (int i = 0; i < R; i++)
{
arr[i] = new int[C];
arr1[i] = new int[C];

}

for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cin >> arr[i][j];
arr1[i][j] = 0;
}
}

for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cout<< arr[i][j] << " ";
}

cout << endl;
}


for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
move(i, j, 0, 0, "");
}
}

}

- Rohan Desai January 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;

#define DEBUG

int **arr, **arr1, R, C;
int maxLandVal = 11;
int maxBlock = 0;

void move(int r, int c, int landVal, int numBlocks, string s)
{
if (arr1[r][c] != 0 && arr1[r][c] <= numBlocks)
return;

++numBlocks;
landVal = landVal + arr[r][c];
arr1[r][c] = numBlocks;

if (landVal > 11 )
return;

if (landVal == maxLandVal && maxBlock < numBlocks)
{
maxBlock = numBlocks;
cout << "land value: " << landVal << endl;
cout << s << endl;
}



if (r < R - 1) move(r + 1, c, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
if (r > 0) move(r - 1, c, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
if (c < C - 1) move(r, c + 1, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
if (c > 0) move(r, c - 1, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");

arr1[r][c] = 0;
}


int main()
{
freopen("Text.txt", "r", stdin);
setbuf(stdout, NULL);
cin >> R;
cin >> C;
arr = new int*[R];
arr1 = new int*[R];

for (int i = 0; i < R; i++)
{
arr[i] = new int[C];
arr1[i] = new int[C];

}

for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cin >> arr[i][j];
arr1[i][j] = 0;
}
}

for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cout<< arr[i][j] << " ";
}

cout << endl;
}


for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
move(i, j, 0, 0, "");
}
}

}

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

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;

#define DEBUG

int **arr, **arr1, R, C;
int maxLandVal = 11;
int maxBlock = 0;

void move(int r, int c, int landVal, int numBlocks, string s)
{
	if (arr1[r][c] != 0 && arr1[r][c] <= numBlocks)
		return;

	++numBlocks;
	landVal = landVal + arr[r][c];
	arr1[r][c] = numBlocks;

	if (landVal > 11 )
		return;

	if (landVal == maxLandVal && maxBlock < numBlocks)
	{
		maxBlock = numBlocks;
		cout << "land value: " << landVal << endl;
		cout << s << endl;
	}



	if (r < R - 1) move(r + 1, c, numBlocks, landVal, s + "[" + to_string(r)  + to_string(c) + "]");
	if (r > 0) move(r - 1, c, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
	if (c < C - 1) move(r, c + 1, numBlocks, landVal, s + "[" + to_string(r)  + to_string(c) + "]");
	if (c > 0) move(r, c - 1, numBlocks, landVal, s + "[" + to_string(r)  + to_string(c) + "]");

	arr1[r][c] = 0;
}


int main()
{
	freopen("Text.txt", "r", stdin);
	setbuf(stdout, NULL);
    cin >> R;
	cin >> C;
	arr = new int*[R];
	arr1 = new int*[R];

	for (int i = 0; i < R; i++)
	{
		arr[i] = new int[C];
		arr1[i] = new int[C];

	}

	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			cin >> arr[i][j];
			arr1[i][j] = 0;
		}
	}

	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			cout<< arr[i][j] << " ";
		}

		cout << endl;
	}

	
	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			move(i, j, 0, 0, "");
		}
	}

}

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

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string>
using namespace std;
#define DEBUG
int **arr, **arr1, R, C;
int maxLandVal = 11;
int maxBlock = 0;
void move(int r, int c, int landVal, int numBlocks, string s)
{
	if (arr1[r][c] != 0 && arr1[r][c] <= numBlocks)
		return;

	++numBlocks;
	landVal = landVal + arr[r][c];
	arr1[r][c] = numBlocks;

	if (landVal > 11 )
		return;

	if (landVal == maxLandVal && maxBlock < numBlocks)
	{
		maxBlock = numBlocks;
		cout << "land value: " << landVal << endl;
		cout << s << endl;
	}



	if (r < R - 1) move(r + 1, c, numBlocks, landVal, s + "[" + to_string(r)  + to_string(c) + "]");
	if (r > 0) move(r - 1, c, numBlocks, landVal, s + "[" + to_string(r) + to_string(c) + "]");
	if (c < C - 1) move(r, c + 1, numBlocks, landVal, s + "[" + to_string(r)  + to_string(c) + "]");
	if (c > 0) move(r, c - 1, numBlocks, landVal, s + "[" + to_string(r)  + to_string(c) + "]");

	arr1[r][c] = 0;
}


int main()
{
	freopen("Text.txt", "r", stdin);
	setbuf(stdout, NULL);
    cin >> R;
	cin >> C;
	arr = new int*[R];
	arr1 = new int*[R];

	for (int i = 0; i < R; i++)
	{
		arr[i] = new int[C];
		arr1[i] = new int[C];

	}

	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			cin >> arr[i][j];
			arr1[i][j] = 0;
		}
	}

	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			cout<< arr[i][j] << " ";
		}

		cout << endl;
	}

	
	for (int i = 0; i < R; i++)
	{
		for (int j = 0; j < C; j++)
		{
			move(i, j, 0, 0, "");
		}
	}
}

- rdrohan.nit January 26, 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