Amazon student Interview Question for Students


Country: United States
Interview Type: Phone Interview




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

By gradually enlarging the difference of two expected numbers....


void mindiff(int n){

int x, y = sqrt(n);
while(true){
if( x*y <= n+2 && x*y >=n )
break;
else if(x*y<n) // suppose x is always bigger number;
x++;
else
y--;
}


}

- Moron March 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lovely answer, i did the same.jus was doing mathematically on a paper drawing a graph(as xy=c is hyper bola)

- Mohit Ahuja March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Trivial solution:
1) x*y can have values only in the set S = {n,n+1,n+2}
2) For each value in set S, find pairwise factors (a,b). Keep a min of |a-b|.

The complexity of finding all pairwise factors of a number 'n' = O(n/2)

- Learner March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Naive approach will be

Get the divisor (x,y ) of n,n+1 and n+2 and then keep track minimum of them s.t |x-y| would be minimum

- NaiveCoder March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Factors {
	public static void main(String args[]) {
		printMinDiffFactors(19); // A = 4 & B = 5
		printMinDiffFactors(100); // A = 10 & B = 10
		printMinDiffFactors(82); // A = 7 & B = 12
		printMinDiffFactors(46); // A = 6 & B = 8
	}

	private static Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();

	public static void printMinDiffFactors(int input) {
		pairs = new HashMap<Integer, Integer>();
		for (int i = 2; i * i <= (input + 2); i++) {
			updatePairs(i, input);
			updatePairs(i, input + 1);
			updatePairs(i, input + 2);
		}

		int minA = input;
		int minB = 1;
		for (Map.Entry<Integer, Integer> a : pairs.entrySet()) {
			if (Math.abs(a.getKey() - a.getValue()) < Math.abs(minA - minB)) {
				minA = a.getKey();
				minB = a.getValue();
			}
		}
		System.out.println("printMinDiffFactors(" + input + "): A = " + minA
				+ " & B = " + minB);
	}

	public static void updatePairs(int aFactor, int input) {
		int count = 0;
		int num = input;
		if (num % aFactor == 0) {

			while (num % aFactor == 0) {
				num = num / aFactor;
				count++;
			}
			int a = 1;
			for (int j = 1; j <= count; j++) {
				a *= aFactor;
				pairs.put(a, input / aFactor);
			}
		}
	}

}

- dev1986 March 19, 2012 | 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