Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

This is a basic form of the game. Sorry if there are any syntax errors, I used a text editor to write the code.Also since this was a console based application I made it as simple as possible. The server reset in this case would be when the game ends. To begin a new session, the game must be restarted.

Class Palindrome
{
	String names;
	Arraylist<String> players = new Arraylist<String>();
	Arraylist<int> scores = new Arraylist<int>();
	boolean gameSession = true;
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		Stromg readLine;
		int index = 0;
		do
		{
			System.out.println("Enter the palindrome word");
			readLine = input.nextLine();
			System.out.println("Enter your name or just hit enter to score your word(score would be added to Guest by default)");
			names = input.nextLine();
			if(names == null)
				name = "Guest"
			index = palindrome(readLine);
			if(players.contains(names))
			{
				scores[players.indexOf(names)] += index;
			}
			else
			{
				players.add(names);
				scores.add(index);
			}
			System.out.println(names + " scored " + index + ". Total score is now " + scores[players.indexOf(names)]);
			Sytem.out.println("Enter 1 to continue current game or 0 to quit and print scoresheet");
			if(input.nextInt != 1)
				gameSession = false;
		}while(gameSession);
  //will print out all the players scores for the hall of fame
		for(int i = 0; i < 5; i++)
		{
			System.out.println(players[i] + " scored " + scores[i]);
		}	
	}

	//Palindrome function takes the input word and returns score
	public int palindrome(String pWord)
	{
		//take out all commonly occuring punctuation marks
		pWord = pWord.replace(' ', '');
		pWord = pWord.replace('.', '');
		pWord = pWord.replace(',', '');
		pWord = pWord.replace("//'", "");
		if(pWord.equals(new StringBuffer(pWord).reverse().toString();)
			return pWord.length;
		else 
			return 0;
	}	
}

- Rhycce October 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note, the printout for the hall of fame is supposed to be the Players arraylist.size instead of 5.

- Rhycce October 03, 2012 | Flag


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