Google Interview Question for Software Engineer / Developers


Team: codeblock
Country: India




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

Class only for the specified problem.

class String 
{
	List<char *> list = null;	//	Doubly linked list.
	
	public String() {
		list = new List();
	}
	public ~String() {
		if(list)
			delete list;
	}
	public String(const char *sentence) {
		char *token = strtoken(sentence);
		while(token) {
			list.append(token);
			token = strtoken(null);	// next token from sentence.
		}
	}
	/**
	*	Runs in o(n) where n is the number of words.
	*/
	bool palindromeCheck()
	{
		if(list.empty())
			return false;
		ListIterator forward = list.iterator(BEGIN);
		ListIterator backward = list.iterator(END);
		
		do
		{
				ListNode n1 = forward.next();
				ListNode n2 = backward.previous();	//	initially both are not at the first/last element.
				if(n1 != n2)
					return false;
		}while(forward < backward)
		
		return true;
	}
	bool ListNode::operator ==(ListNode &n)
	{
		//	compare contents.
	}
	bool ListNode::operator ==(ListNode &n)
	{
		//	compare contents
	}
	
	
};

- Noobie October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could you use #include<string> ?

class String{
private:
	string m_data;
public:
	String()
	{
		cout << "insert string: ";
		getline(cin, m_data);
		cout << endl;
	}
	void CheckIfPalindrome() 
	{
		string revSentence = "";
		string word = "";
		
		for ( int i = 0; i < m_data.size(); ++i) {
			word += m_data[i];
			if ( m_data[i] == ' ') {
				revSentence = word + revSentence;	
				word = "";
			}
		}
		//last word
		revSentence = word + " " + revSentence;	
		
		cout << revSentence << endl;
	}
};

- Gianluca October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find the mid of the sentence..
From beginning to mid of the original sentence do the following:
1] Reverse the sentence
2] Reverse the words
Now, compare the first half with the second half of the original sentence. They should be same if its a palindrome.

- coderGirl October 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java implementation...

public class ReverseSentance {

	String orgStr="";
	String revStr="";
	/**
	 * @param args
	 */
	
	public ReverseSentance(String[] inStr)
	{
		for(String s: inStr)
			orgStr=orgStr+s+" ";
		orgStr=orgStr.substring(0, orgStr.length()-1);
		
		int len = inStr.length;
		for(int i = len-1; i>=0; i--)
		{
			revStr= revStr+inStr[i]+" ";
		}
		revStr=revStr.substring(0, revStr.length()-1);
	}
	
	public boolean checkForPalendrome()
	{
		boolean flag = true;
		int i=0;
		for(i=0;i < orgStr.length(); i++)
		{
			if(orgStr.charAt(i) != revStr.charAt(i))
				break;
		}
		if(i==orgStr.length())
			return true;
		else
			return false;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String[] a = {"wife", "loves", "husband",",", "husband", "loves", "wife"};
		ReverseSentance reverseObj =  new ReverseSentance(a);	
		System.out.println(reverseObj.revStr);
		
		
		if(reverseObj.checkForPalendrome())
		{
			System.out.println("it's Palendrome.. :)");
		}else{
			System.out.println("it's Not palendrome..   :( ");
		}
		
	}

}

- Venkat Edala November 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Example1 {

public static void main(String[] args) {

try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sentence = br.readLine();
String[] words = sentence.split(" ");
for(int i=0;i<words.length/2;i++)
{
String temp= words[i];
words[i]=words[words.length-1-i];
words[words.length-1-i]=temp;

}
String s="";
for(int j=0;j<words.length;j++)
{
if(j==words.length-1)
s=s+words[j];
else
s=s+words[j]+" ";
}

System.out.println(s);
if(s.equals(sentence))
System.out.println("Yes Pallingdrome");
else
System.out.println("No Pallingdrome");
}
catch(Exception e)
{
e.printStackTrace();
}

}


}

- Saurabh January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class String:
def __init__(self, sentence):
nospaces = ' '.join(sentence.split())
self.words = nospaces.split(' ')

def palindromecheck(self):
if not self.words:
return True

indexInit = 0
indexEnd = len(self.words) - 1

while indexInit <= indexEnd:
if self.words[indexInit] != self.words[indexEnd]:
return False
indexInit += 1
indexEnd -= 1
return True

- rchg1988 July 24, 2015 | 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