Amazon Interview Question for Software Analysts


Country: India
Interview Type: In-Person




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

I know its a DP problem but how to proceed ?

- ram188490 September 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

given : array a with index 0 to n-1 for n portal
a[i] = value on portal
lets x = portal being activated
int manaPoint = 0;
for(r=n-1,x=r-1,l=x-1; x>1 ; x--,l--){
manaPoint = a[l] * a[r];
}
ans is 12 for input {1,2,3,4}

- Anonymous September 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here array is sorted as thats given

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

no nothing is given about sorted array...it may be in any order
Consider test case
2 100 19 20 10 9 100
Output 15200

- ram188490 September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static int max_score(ArrayList<Integer> unused, int[] values, int sum){
		if(unused.size() <= 1){
			return values[unused.get(0) - 1]*values[unused.get(0) + 1] + sum;
		}
		
		else{
			int max = 0;
			for(int i:unused){
				System.out.println("The value of i is :" + i);
				ArrayList<Integer> tmp = new ArrayList<>();
				for (int j = 0; j < unused.size(); j ++){
					if (unused.get(j) != i ){
						tmp.add(unused.get(j));
					}
				}
				System.out.println("The value of tmp(0) is :" + tmp.get(0));
				int left = i - 1;
				int right = i + 1;
				System.out.println(tmp.contains(right));
				while (tmp.contains(left) && left>0){
					left --;
				}
				while (tmp.contains(right) && right < values.length - 1){
					right ++;
				}
				System.out.println("The left is " + left + "; The right is " + right);
				//System.out.println("The length of tmp is " + tmp.size());
				int tmp_s = sum + values[left] * values[right];
				int result = max_point.max_score(tmp, values, tmp_s);
				if (result > max){
					//System.out.println("The current max is: " + result);
					max = result;
				}
			}
			//Collections.sort(results);
			return max;
		}

}

Consider the problem reversely, you can image inserting elements between two existing elements, say at the very last, the ending two elements must be multiplied, so at the first step you insert one arbitrary element (the uninserted elements's indexes are stored in the unused list )between the two ending ones. Then recursively inserting unused elements, until the unused list is empty (in other words you restore the original list).

- tt September 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think your test case is wrong. The question states, the doors are numbered from 0 to n-1. Your test case has 1 2 3 4, shouldn't it be a permutation of 0, 1, 2, 3 ?

- belligerentCoder September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

well..it looks like a simple backtracking..so here is what we should do.we select every possible permutation of doors 2 to n-1(since 1 and n cannot be opened so no point considering them) .so we have to find the permutations of n-2 doors excluding 1 and n.

#include<cstdio>
#include<iostream>
#include<vector>
#include<bitset>
#include<cstring>
#define UL unsigned long
using namespace std;
int a[100],n,maxi=0;
bool vis[100];
void dfs(int l,int val,int c)
{
    
    if(c==n-2)
    {
        
        maxi=max(val,maxi);
        return;
    }
    else
    {
        vis[l]=true;

        int j,k;
        for(int i=1;i<n-1;i++)
        {
            if(!vis[i])
            {
                for(j=i-1;vis[j] && j>=0;j--);
                for(k=i+1;vis[k] && k<n;k++);
                dfs(i,val+a[j]*a[k],c+1);
            }
        }
        vis[l]=false;
    }


}
int main()
{
int i,j;

cin>>n;
for(i=0;i<n;i++)
{
    cin>>a[i];
    }
    memset(vis,0,100*sizeof(bool));
    for(i=1;i<n-1;i++)
    {
        if(!vis[i])
        {

            dfs(i,a[i-1]*a[i+1],1);
          
        }
    }

cout<<maxi;
return 0;
}

so here the function dfs maintains the current value(val) and the number of doors already activated(c).whenever c becomes n-2 ie all the doors have been activated we store the val in maxi(only if is greater than current maxi) and then we backtrack.Similarly we keep a track of the activated door numbers using a boolean array vis[] so that we donot activate the same door again.
all suggestions to improve the solution further are welcome.

- rock2321 October 01, 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