## ThoughtWorks Interview Question for Software Engineer / Developers

Country: India
Interview Type: Written Test

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

``````import java.util.*;

public class ThoughtWorks {
static int countBits(int n) {
int count = 0;
while (n != 0) {
count++;
n >>= 1;
}

return count;
}

public static boolean isWinner(int n, int p) {
return n == p || n - 1 == p || n + 1 == p;
}

public static void main(String args[]) {
try (Scanner sc = new Scanner(System.in)) {
int r = sc.nextInt();
for (int t = 0; t < r; t++) {
int n = sc.nextInt();
int bits = countBits(n);
int turn = 0, p = n;

boolean isFound = false;
bits--;
while (bits >= 0) {
turn++;
boolean bit = (p & (1 << bits))!=0;
p ^= (1 << bits);
boolean leftbit = (p & (1 << (bits + 1)))!=0;

if (leftbit== bit)
p ^= (1 << (bits + 1));
if (bits > 0) {
boolean rightbit = (p & (1 << (bits - 1)))!=0;

if (rightbit == bit)
p ^= (1 << (bits - 1));
}
System.out.println("p "+p);
if (isWinner(n, p)) {
isFound = true;
break;
}
bits--;

}
if (!isFound)
turn++;
System.out.println((turn % 2 == 1) ? "X" : "Y");
}
}
}
}

Could any body help me why this code is not passing all test cass``````

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

``````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");
}

}
}``````

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

``````#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;
}
}``````

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

``````#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;
}
}``````

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.

### 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.