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

- harshit.knit July 19, 2018 | Flag Reply
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");
		}

	}
}

- Deepak June 16, 2019 | 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