Amazon Interview Question for Software Engineer in Tests


Country: India
Interview Type: In-Person




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

Using DP we can solve this.

Code:

public void findMinCostAndQuantity(int q[], int p[], int tot) {
		int c[] = new int[tot + 1];
		int price[] = new int[tot + 1];
		for (int i = 0; i <= tot; i++) {
			c[i] = i;
			price[i] = 10000000;
		}
		for (int i = 1; i <= tot; i++) {
			for (int j = 0; j < q.length && q[j] <= i; j++) {
				if (i == q[j] && price[i] > p[j]) {
					c[i] = 1;
					price[i] = p[j];
				}
				if ((price[i] > price[i - q[j]] + p[j])
						|| (price[i] == price[i - q[j]] + p[j] && (c[i] > c[i
								- q[j]] + 1))) {

					price[i] = price[i - q[j]] + p[j];
					c[i] = c[i - q[j]] + 1;
				}
			}
		}
		System.out.println(c[tot]);
		System.out.println(price[tot]);
	}

Here I have kept the price to be optimum and then the quantity.

- Dhass April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The idea is good. I think both the arrays c[] and price[] should be initialized with values = Integer.MAX_VALUE and c[0] and price[0] should be 0. And I also feel that
if (i == q[j] && price[i] > p[j])
{
c[i] = 1;
price[i] = p[j];
}
need not be handled separately.

- India April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain the algorithm?

- sk August 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How can both be optimum at same time ??

- Abhishek April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How to we define the optimal combination??? For example say quantity[] = 1,5,10
and corresponding price[] = 10,30,80 . Now if total quantity is 20. We have 3 options
1) 20 bags cost:200. 2)4 bags Cost 120 and 3.) 2bags and cost160 and many more possibilities. In the above defined options which gets priority 2 or 3???

- India April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes India is right here
It should be either on price or on quantity. I think price should matter here as given that it should have Q quantity

- DashDash April 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1: Get all the possible combinations of quantity which is equal to Q.
Step 2: Get the minimum amount combination from above combinations.
Step 3: If more than one combination have same price then get the one with lower number of bags.

- GT1 October 05, 2013 | 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