## ThoughtWorks Interview Question

Software Engineer / Developers**Country:**India

**Interview Type:**Written Test

```
import java.util.*;
public class Hello {
public static String binaryValue(int numberGiven)
{
String binary="";
while(numberGiven>0)
{
binary+=numberGiven%2; //0 0 0 1
numberGiven/=2; //4 2 1
}
StringBuilder sb=new StringBuilder(binary).reverse();
return sb.toString();
}
public static boolean isGivenNumber(char[] binary,int numberGiven)
{
if(Integer.parseInt(String.valueOf(binary),2)==numberGiven || Integer.parseInt(String.valueOf(binary),2)==numberGiven+1 || Integer.parseInt(String.valueOf(binary),2)==numberGiven-1)
return true;
else
return false;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int rounds=s.nextInt();
for(int index=0;index<rounds;index++)
{
int numberGiven = s.nextInt();
char[] binary = binaryValue(numberGiven).toCharArray();
int move=0,flag=0;
while(move<=binary.length)
{
move++;
for(int i=0;i<binary.length;i++)
{
if(i==move-1)
{
if(binary[i]=='1')
binary[i]='0';
else
binary[i]='1';
if(i-1>=0 && binary[i-1]!=binary[i])
binary[i-1]=binary[i];
if(i+1<binary.length && binary[i]!=binary[i+1])
binary[i+1]=binary[i];
break;
}
}
if(isGivenNumber(binary,numberGiven))
{
if(move%2!=0)
{
System.out.println("Xavier");
flag=1;
break;
}
else
{
System.out.println("Yale");
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag==0)
System.out.println("Draw");
}
}
}
```

```
#include<bits/stdc++.h>
#include<vector>
using namespace std;
vector<int> convert (int n)
{
vector<int> v;
while(n!=0)
{
v.push_back(n % 2);
n = n / 2;
}
reverse(v.begin(),v.end());
return v;
};
int convert_dec (vector<int> v)
{
reverse(v.begin(),v.end());
int sum = 0;
for(int i=0; i<v.size(); i++)
{
sum += v[i]*pow(2,i);
}
return sum;
};
int main()
{
int r,n,p,i;
cin>>r;
while(r!=0){
vector<int> v;
cin>>n;
v = convert(n);
p = v.size();
int f=0;
for(i=0; i<p; i++)
{
if(i-1!=-1 && i+1<p)
{
if(v[i] == v[i-1] && v[i] == v[i+1])
{
v[i] = 1 - v[i];
v[i+1] = 1 - v[i+1];
v[i-1] = 1 - v[i-1];
f = i;
}
else if(v[i] == v[i-1] && v[i] != v[i+1])
{
v[i] = 1 - v[i];
v[i-1] = 1 - v[i-1];
f = i;
}
else if(v[i] != v[i-1] && v[i] == v[i+1])
{
v[i] = 1 - v[i];
v[i+1] = 1 - v[i+1];
f = i;
}
}
else if(i-1 == -1)
{
if(v[i] == v[i+1])
{
v[i] = 1-v[i];
v[i+1]= 1-v[i+1];
f = i;
}
}
else if(i+1 == p)
{
if(v[i] == v[i-1])
{
v[i] = 1-v[i];
v[i-1]= 1-v[i-1];
}
f = i;
}
}
int x = convert_dec(v);
if(x == n || abs(x-n)==1)
{
cout<<(f%2==0 ? "X" : "Y" )<<endl;
}
r-=1;
}
}
```

```
#include<bits/stdc++.h>
#include<vector>
using namespace std;
vector<int> convert (int n)
{
vector<int> v;
while(n!=0)
{
v.push_back(n % 2);
n = n / 2;
}
reverse(v.begin(),v.end());
return v;
};
int convert_dec (vector<int> v)
{
reverse(v.begin(),v.end());
int sum = 0;
for(int i=0; i<v.size(); i++)
{
sum += v[i]*pow(2,i);
}
return sum;
};
int main()
{
int r,n,p,i;
cin>>r;
while(r!=0){
vector<int> v;
cin>>n;
v = convert(n);
p = v.size();
int f=0;
for(i=0; i<p; i++)
{
if(i-1!=-1 && i+1<p)
{
if(v[i] == v[i-1] && v[i] == v[i+1])
{
v[i] = 1 - v[i];
v[i+1] = 1 - v[i+1];
v[i-1] = 1 - v[i-1];
f = i;
}
else if(v[i] == v[i-1] && v[i] != v[i+1])
{
v[i] = 1 - v[i];
v[i-1] = 1 - v[i-1];
f = i;
}
else if(v[i] != v[i-1] && v[i] == v[i+1])
{
v[i] = 1 - v[i];
v[i+1] = 1 - v[i+1];
f = i;
}
}
else if(i-1 == -1)
{
if(v[i] == v[i+1])
{
v[i] = 1-v[i];
v[i+1]= 1-v[i+1];
f = i;
}
}
else if(i+1 == p)
{
if(v[i] == v[i-1])
{
v[i] = 1-v[i];
v[i-1]= 1-v[i-1];
}
f = i;
}
}
int x = convert_dec(v);
if(x == n || abs(x-n)==1)
{
cout<<(f%2==0 ? "X" : "Y" )<<endl;
}
r-=1;
}
}
```

- harshit.knit July 19, 2018