Microsoft Interview.
Company: Microsoft
Role: sde
Interview Stage: unknown
Location: unknown
Year: unknown
Original Post Content:
I was interviewed at Microsoft and at that time I was not able to code this solution, but when I tried afterwards I was able to.
So the question was:
You are given 3 integers aa=5, ab=0, bb=2.
You have to use 3 strings "AA", "AB", and "BB".
We need to create a string of maximum length where the string concatenation should not create "AAA" or "BBB" together.
Attaching the solution for the problem, which I solved.
Anyone who wants to update the approach is welcomed.
public class MyClass {
public static void main(String args[]) {
//aa ab bb
System.out.println(sol(9,0,5));
}
public static String sol(int A, int B,int C) {
StringBuilder res = new StringBuilder();
int i = A, j = B, k = C;
String str="";
//3-2-1
while (i>0&&j>0&&k>0) {
if(i>k && i>j){
str+="AABBAB";
i--;j--;k--;
}else if(j>i &&j>k){
str+="ABAABB";
i--;j--;k--;
}else{
str+="BBABAA";
i--;j--;k--;
}
}
if(i==0){
while(j>0 && k>0){
str+="BBAB";
j--;k--;
}
if(j!=0){
if(str.isEmpty()){
str+="AB";
j--;
}{
while(j>0 && str.charAt(str.length()-1)!='A'){
str+="AB";
j--;
}
}
}
if(k!=0){
if(str.isEmpty()){
str+="BB";
k--;
}
while(k>0 && str.charAt(str.length()-1)!='B'){
str+="BB";
k--;
}
}
}if(j==0){
while(i>0&&k>0){
str+="AABB";
i--;k--;
/>
}if(i>0 && !str.isEmpty() && str.charAt(str.length()-1)!='A' ){
str+="AA";
i--;
}
}if(k==0){
while(j>0&&i>0){
str+="ABAA";
j--;i--;
}
}
return str;
}
}