Amazon Interview Question for Backend Developers


Country: India




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

why not 4x3 for 13? 4x3=12 is closer to 13 than 5x3=15

- Anonymous July 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

because question is to find maximally close not minimally

- Anonymous September 03, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class output {
private int length;
private int breadth;
}
public output finddimension(input) {
int l=1;
int b=input;
int check=0;
while(check==0) {
l++;
if(l==b) {
check=1;
}
if(input%l=0) {
b=input/l;
}
}
return new output(l,b);
}

- nirmalpoonattu July 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Nirmalpoonattu: Scumbag solution

- Vilu patukaran July 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void findClosest(int n)
{
double sq = Math.sqrt(n);
if((Math.floor(sq) * Math.ceil(sq) >= n))
cout<< Math.floor(sq) << Math.ceil(sq)<<endl;
else
cout<< (Math.floor(sq) -1) << Math.ceil(sq)<<endl;
}

- hprem991 July 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def trivialClosest(n):
    return 1, n 

import sys
import math

def closest(n) :
    lowestDiff = sys.maxint
    lowestPair = [-1, -1]
    iterations = 0
    for i in xrange (2, int((math.sqrt(n)+1))) :
            if n % i == 0 :
                return (i, n/i)
    return (2, n/2)

- colin August 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//============================================================================
// Name        : MaximalCloseFactor.cpp
// Author      : Nitish Raj, Scientist DRDO,raj.nitp@gmail.com
// Version     :
// Copyright   : Your copyright notice
// Description : MaximalCloset, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

void findingMaximalCloset(int data, int &fac1 , int &fac2){
    if(data == 1) return;
	fac1 = 1;
	fac2 = data;
	bool flag = true;
	while(flag)
	{
		fac1++;
		if(fac1 == fac2) flag = false;
		if(data%fac1 == 0)
		{
			fac2 = data/fac1;
			if(fac1 == fac2) flag = false;
			//cout<<"FAC 1 "<<fac1<<" FAC2 "<<fac2<<endl;
		}

	}

	if(fac2 == 1){
		data = data + 1;
		fac1 = 1;
		findingMaximalCloset(data,fac1,fac2);
	}
	else if(fac1 != 1 && fac2 != 1) return;

}

int main() {
	int data,fac1 = 1,fac2 = 1;
	cout << "Enter an integer for finding maximal close factor" << endl; // prints
	cin>>data;

	findingMaximalCloset(data,fac1 ,fac2);
	cout<<"FACTORS ARE "<<fac1<<" * "<<fac2<<endl;

	 return 0;
}

- raj.nitp@gmail.com August 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import sys

def closest_factors(n):
    min_score = sys.maxint
    min_pair = None
    for i in range(1, int(n**0.5)+1):
         _i,_j = i,n//i
         if _j*_i == n or _j-_i == 0:
             continue
         delta = _j-_i
         dist = n-_j*_i
         score = delta*dist
         if score < min_score:
             min_score = score
             min_pair = _i,_j
    return min_pair

- aoisagi August 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Task is incorrect.
Probably it is required to find such I and B where B+I is minimum.

public class TestClass{
    public static void main(String[] args) {
        int n = 12;
        int i = (int) Math.sqrt((double) n);
        int b = (int) Math.round((double) n / (double) i);

        System.out.println(n + ": " + i + "x" + b);
    }
}

- mger1979 August 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
* Created by arupdutta on 27/08/16.
* 1. get floored square root value of the number e.g. for 12 we get a value of 3
* 2. set the value in two variables, x = y = sqrt(num)
* 3. while x>=1 & y<=num check
* if x*y = num return x & y
* else if x*y <num increment y
* else decrement x
* 4. if loop break then return 1 and num
*/

import java.util.Scanner;
public class MaximumFactor {
    public int[] getMaximalFactors(int num){
        int[] factors = new int[2];
        factors[0] = 1;
        factors[1] = num;

        int x, y;
        x = y = (int)Math.sqrt(num);

        while(x>=1 && y<=num){
            int product = x * y;
            if( product == num){
                factors[0] = x;
                factors[1] = y;
                return factors;
            } else if(product < num){
                y++;
            } else {
                x--;
            }
        }

        return factors;
    }

    public static void main(String[] args){
        MaximumFactor m = new MaximumFactor();
        Scanner s = new Scanner(System.in);
        int[] factors = m.getMaximalFactors(s.nextInt());
        System.out.println("Maximal factors: " + factors[0] + "\t" + factors[1]);
    }
}

- arup.me August 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first_dimension = SQRT(value)
second_dimension = value/first_dimension
if second_dimension != ABS(second_dimension)
{ second_dimension =+1 }
return first_dimension x (second_dimension)

- Anonymous August 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

dei badu

- Anonymous July 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

def trivialClosest(n):
    return 1, n 

import sys
import math

def closest(n) :
    lowestDiff = sys.maxint
    lowestPair = [-1, -1]
    iterations = 0
    for i in xrange (2, int((math.sqrt(n)+1))) :
            if n % i == 0 :
                return (i, n/i)
    return (2, n/2)

- colin August 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.


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