AMD Interview Question for Students


Country: India
Interview Type: Written Test




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

Stating this as optimization problem: Find max non decreasing sequence so that each species contributes at least one element. If max length is equal to length of species, answer to original question is TRUE.
Trying to solve this as simple DP might yield false positive results when all elements are taken from only one of the species, e.g. {{2,2,3,3},{4,1,1,1}}, to avoid this we will induce fake obstacles, a different pair of indexes from species A an B. This will force DP to make a switch at least once.
Overall complexity will be O(n^3).

private static final int INVALID = 1011;
	
	public static boolean isSimilar(int[] a, int[] b) {
		int len = a.length;
		for (int i = 0; i<len; i++) {
			int atmp = a[i];
			a[i] = INVALID;
			for (int j = 0; j < len; j++) {
				if (i==j) { //no point exploring this setup since 
							//it will definitely not produce positive result
					continue;
				}
				int btmp = b[j];
				b[j] = INVALID;
				if (processSim(a, b) == true) {
					return true;
				}
				b[j] = btmp;
			}
			a[i] = atmp;
		}
		
		return false;
	}
	
	private static boolean processSim(int[] a, int[] b) {
		int len = a.length;
		
		int[][] maxLenMemo = new int[len+1][2];
				
		maxLenMemo[0][0] = 0;
		maxLenMemo[0][1] = 0;
		maxLenMemo[1][0] = 1;
		maxLenMemo[1][1] = 1;

		for (int i = 2; i <= len; i++) {
			
			int maxLen = 0;
			
			if (a[i-1] != INVALID) {
				if (a[i-1] >= a[i-2])
					maxLen=(maxLen<maxLenMemo[i-1][0] + 1)?(maxLenMemo[i-1][0] + 1):maxLen;
				
				if ( a[i-1] >= b[i-2]) 
					maxLen=(maxLen<maxLenMemo[i-1][1] + 1)?(maxLenMemo[i-1][1] + 1):maxLen;
			
				maxLenMemo[i][0] = maxLen;
			}
			maxLen=0;
			
			if (b[i-1] != INVALID) {
				if (b[i-1] >= b[i-2]) 
					maxLen = (maxLen<maxLenMemo[i-1][1] + 1)?(maxLenMemo[i-1][1] + 1):maxLen;
				
				if (b[i-1] >= a[i-2]) 
					maxLen = (maxLen<maxLenMemo[i-1][0] + 1)?(maxLenMemo[i-1][0] + 1):maxLen;

				maxLenMemo[i][1] = maxLen;
			}
		}
		
		return Math.max(maxLenMemo[len][0], maxLenMemo[len][1]) == len;
	}

- blckembr December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++, recursion, O(2 ^ n)

#include <iostream>
#include <vector>

using namespace std;

bool nonDecreasingSequence(vector<int>& a, vector<int>& b, vector<int>&c, int idx, int bit) {
	if (idx == a.size()) {
		return bit == 3;
	}

	if (idx == 0 || a[idx] >= c[idx - 1]) {
		c[idx] = a[idx];
		if (nonDecreasingSequence(a, b, c, idx + 1, bit | 1) == true) return true;
	}

	if (idx == 0 || b[idx] >= c[idx - 1]) {
		c[idx] = b[idx];
		if (nonDecreasingSequence(a, b, c, idx + 1, bit | 2) == true) return true;
	}

	return false;
}

int main(int argc, char* argv[]) {
	vector<int> a, b, c;
	int t, n, i;

	cin >> t;
	while (t--) {
		cin >> n;
		a.assign(n, 0);
		b.assign(n, 0);
		c.assign(n, 0);

		for (i = 0; i < n; i++) cin >> a[i];
		for (i = 0; i < n; i++) cin >> b[i];

		if (nonDecreasingSequence(a, b, c, 0, 0) == true) {
			for (i = 0; i < n; i++) {
				cout << c[i] << " ";
			}
			cout << "\n";
		} else {
			cout << "IMPOSSIBLE\n";
		}
	}

	return 0;
}

- kyduke December 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@kyduke, Have you considered a DP solution?

- blckembr December 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Greedy.

- NotImplemented December 18, 2015 | 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