Amazon Interview Question for SDE1s


Country: India




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

private static int sqaurePlotToBuy(int minApples) {
double totalApplesInSquare = 0;
int unit =0;
while(minApples>totalApplesInSquare){
unit++;

totalApplesInSquare +=12*Math.pow(unit,2);
}

return unit*8;
}

- shashi July 13, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sir your code is working fine. can you explain the idea behind this

- gsaini.cse September 25, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sir .. can you explain this concept?

- priyanka December 12, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

each increase in half side of square results in increase in number in square by 12*(2**half_side)

- APURV RATHORE August 31, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

each increase in half side of square results in increase in number in square by 12*(2**half_side)

- APURV RATHORE August 31, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

each increase in half side of square results in increase in number in square by 12*(2**half_side)

- apurv August 31, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question is not well explained. No offense, but you guys are ruining this website.

Is input same as X ? squared plot centered at (0,0) => How can it be centered at 0,0 since its the corner of the grid !! Do you mean plot starts at (0,0) and it has to be square ?

If thats the case, then Grid can be converted by adding values in square :
0 1 2 0 1 2
3 4 5 => 3 8 5
6 7 8 6 7 36

This grid has 3 squares starting at 0,0. size 1x1, 2x2 and 3x3. is that what you mean ?

- Code reviewer June 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The input is X.
Which is the minimum number of apples that should be in the plot.
By i,j they mean points in catrtesian system. At a point i,j number of apples found is |i| +|j|.
The plots (square) would be centered at 0,0 and can be of certain half lengths 1,2,3,4 i.e., with side lengths 2,4,6,etc..
For the plot of minimum size, it would have perimiter 8 units and number of apples would be the sum of absolute value of coordinates both on the perimeter and inside the square.

- Yashwardhan Singh July 17, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As the square plot is centred around (0, 0), we can't have a plot of odd dimensions (seeing test cases, I guess that the coordinates are integral). Now when we try to get the sum of points on the boundary for dimension = 2, 4 etc, we see that the sum is 3 * (dimension) * (dimension).
From now on, let me take n as the dimension. We have to find least n such that 3 * sum of squares of even numbers till n is greater than or equal to our input. The sum of squares of first n natural numbers can be calculated using 2n(n+1)(2n+1)/3. Thus we have to find least n such that 2n(n+1)(2n+1) is less than or equal to our input (Note that the outer 3 got cancelled with the denominator). Do a binary search over n and get it!). Answer = 4 * n (we have to return perimeter).

- Naman Bhalla June 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why do we have to take sum of 3*(dimension)*(dimension)?
like for plot dimension=2, sum of apples=8, and for dimension=4, it is comes out to be 3*(4)*(4)=48, so why to take their sum?
if we take their sums, wouldn't it mean that for n=4,total apple the plot can contain=(48+8)=56, while according to me, it must be 48 only?

- Aryan Poddar July 23, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{public static int CalculatePerimeter(int minCount)
{
if (minCount <= 0) throw new ArgumentException(nameof(minCount));

var count = 0.0;
var unit = 0;
while (count < minCount)
{
unit++;

count = 2 * unit * Math.Pow(unit + 1, 2);
}

return unit;
}
}}

- BennY July 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{

public static int CalculatePerimeter(int minCount)
{
if (minCount <= 0) throw new ArgumentException(nameof(minCount));

var count = 0.0;
var unit = 0;
while (count < minCount)
{
unit++;

count = 2 * unit * Math.Pow(unit + 1, 2);
}

return unit;
}

}}

- Benn Y July 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use binary search.
1. Double the half square side. When we reach the number of collected apples, it stops.
2. Use the binary search to get the minimal perimeter.

int CalculateApples(int x)
{
	int result = 0;
	for (int i = -x+1; i <= x-1; i++)
	{
		result += abs(i) + x;
	}

	result = result * 4 + (x+x)*4;

	return result;
}

int GetMinimalPerimeter(int X)
{
	int result;

	int cur = 1;
	while (true)
	{
		int num_apples = CalculateApples(cur);
		if (num_apples == X)
		{
			result = 8 * cur;
			return result;
		}

		if (num_apples > X)
		{
			result = 8 * cur;
			break;
		}
		cur *= 2;
	}

	if (cur == 1) return result;

	int left = cur / 2, right = cur;
	while (left <= right)
	{
		int m = left + (right - left) / 2;
		int num_apples = CalculateApples(m);
		if (num_apples == X)
		{
			result = 8 * m;
			return result;
		}
		else if (num_apples > X)
		{
			result = 8 * m;
			right = m - 1;
		}
		else
		{
			left = m + 1;
		}
	}

	return result;
}

- LANorth July 07, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Chutiya

- Ayush K September 29, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int findPeri(int apples) {
		int sum=0;
		int x[] = {-1,-1,-1,0,0,1,1,1};
		int y[] = {-1,0,1,-1,1,-1,0,1};
		
		for (int i=0;i<8;++i) {
			x[i]<0?(x[i]*=-1):x[i];
			y[i]<0?(y[i]*=-1):y[i];
		}
		int factor=1;
		while(sum<apples) {
			for (int i=0;i<8;++i){
				sum += x[i]+y[i];
			}
			if(sum>=apples) break;
			++factor;
			for (int i=0;i<8;++i) {
				x[i]*=factor; y[i]*=factor;
			}
		}
		cout<<endl<<"Sum:"<<sum;
		return factor<<3;
}

int main() {
	int a; cin>>a;
	int p = findPeri(a);
	cout<<endl<<"Perimeter:"<<p;
	return 0;
}

- alanturing06022014 July 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//"apple_req ":-  no of minimum apple required
//" n"  :-  Order of matrix i.e. size of Garden

int perameter(int apple_req, int n){
int p = 0;
int total_apple = 0;

if(apple_req >= 4){

	for(p = 1; p<=n; p++){
//     pow(4,i)*(2*n-p): - will give maximum number of apples in square Garden with side of size p
	if(apple_req <= pow(4,i)*(2*n-p))
			break;
	}

return 4*p;
}

- Ajay Kumar October 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void cal_apples(int len, int &perimeter, int &num_apples)
{
int x_init= -len;
int y_init = -len;
int x_fin = len;
int y_fin = +len;

for (int i=x_init; i<=x_fin; i++)
{
for (int j = y_init; j<=y_fin; j++)
{
cout << "i : " << i << ", j : " << j << endl;
num_apples += abs(i) + abs(j);
}
}
perimeter = len*2*4;
}

int main()
{
int len = 0;
int perimeter = 0;
int num_apples = 0;
int num_min_apples = 61;

while (!(num_min_apples <= num_apples))
{
len++;
perimeter = 0;
num_apples = 0;
cal_apples(len, perimeter, num_apples);
cout << "len : " << len << endl;
}

cout << "Num of apples : " << num_apples << endl;
cout << "Perimeter : " << perimeter << endl;

return 0;
}

- cmb April 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void cal_apples(int len, int &perimeter, int &num_apples)
{
int x_init= -len;
int y_init = -len;
int x_fin = len;
int y_fin = +len;

for (int i=x_init; i<=x_fin; i++)
{
for (int j = y_init; j<=y_fin; j++)
{
cout << "i : " << i << ", j : " << j << endl;
num_apples += abs(i) + abs(j);
}
}
perimeter = len*2*4;
}

int main()
{
int len = 0;
int perimeter = 0;
int num_apples = 0;
int num_min_apples = 61;

while (!(num_min_apples <= num_apples))
{
len++;
perimeter = 0;
num_apples = 0;
cal_apples(len, perimeter, num_apples);
cout << "len : " << len << endl;
}

cout << "Num of apples : " << num_apples << endl;
cout << "Perimeter : " << perimeter << endl;

return 0;
}

}}

- cmb April 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void cal_apples(int len, int &perimeter, int &num_apples)
{
int x_init= -len;
int y_init = -len;
int x_fin = len;
int y_fin = +len;

for (int i=x_init; i<=x_fin; i++)
{
for (int j = y_init; j<=y_fin; j++)
{
cout << "i : " << i << ", j : " << j << endl;
num_apples += abs(i) + abs(j);
}
}
perimeter = len*2*4;
}

int main()
{
int len = 0;
int perimeter = 0;
int num_apples = 0;
int num_min_apples = 61;

while (!(num_min_apples <= num_apples))
{
len++;
perimeter = 0;
num_apples = 0;
cal_apples(len, perimeter, num_apples);
cout << "len : " << len << endl;
}

cout << "Num of apples : " << num_apples << endl;
cout << "Perimeter : " << perimeter << endl;

return 0;
}

- cmb April 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n=int(input())
i=1
ans=0
while True:
for j in range(i+1):
x=i*10 +j
ans=ans+(i+j)*4

if ans>=n:
print(i*8)
break
i+=1

- anurag.7140 December 07, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int i=1,t;
t=((8+n)-(8+n)%4)/4;
if(t%2==0)
t=t*4;
else
t=(t-1)*4;
if(n%8==0)
t= n;
cout<<t;
return 0;
}

- ajin December 14, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int i=1,t;
t=((8+n)-(8+n)%4)/4;
if(t%2==0)
    t=t*4;
else
    t=(t-1)*4;
if(n%8==0)
    t= n;
cout<<t;
return 0;
}

- ajin December 14, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int i=1,t;
t=((8+n)-(8+n)%4)/4;
if(t%2==0)
    t=t*4;
else
    t=(t-1)*4;
if(n%8==0)
    t= n;
cout<<t;
return 0;
}

- ajin December 14, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

- Anonymous January 27, 2022 | 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