Crompton Greeves Interview Question for Software Engineer / Developers


Country: India




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

for small k there is a dp solution below - complexity O(2^k*n^2) - polinomial on n

import java.util.Arrays;

public class Main1 {
	static final int K = 3;
	static class Solution{
		final String s;
		final int [][][] memo;
		Solution(String s){
			this.s = s;
			this.memo = new int[1 << (K + 1)][K + 1][s.length()];
			for (int [][] m : memo){
				for (int [] m1 : m){
					Arrays.fill(m1, -1);
				}
			}
		}
		int solve(){
			return rec(0, 0, 0);
		}
		int rec(int mask, int last, int i){
			if (i == s.length()){
				return 0;
			}
			if (last != -1 && memo[mask][last][i] != -1){
				return memo[mask][last][i];
			}
			int j = s.charAt(i) - 'a' + 1;
			int res = 0;
			if (last == j){
				res = Math.max(res, rec(mask, last, i + 1) + 1);
			}else if ((mask & (1 << j)) == 0){
				res = Math.max(res, rec(mask | (1 << j), j, i + 1) + 1);				
			}
			res = Math.max(res, rec(mask, last, i + 1));
			return memo[mask][last][i] = res;
		}
	}
	
	public static void main(String[] args){
		System.out.println(new Solution("aaaccaaaccbccbbbab").solve());
	}
}

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

Alternatively you can find LCS for each permutation of aaa,bbb,ccc

- Anonymous July 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

FFFF

- F November 20, 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