Interview Question


Country: United States




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

Your final solution looks good to me. You could place some of the work with built in functions

My solution in C++ (you can replace isalpha with Character.isLetter(char) and tolower with Character.toLowerCase(char) in java if you like))

bool isPal(const char* str)
{
	int f=0;
	int b=strlen(str)-1;
	while(f>b)
	{
		char front=tolower(str[f]); //normalize for upper vs lower comp
		while(!isalpha(front) && f>b) 	//skip non alpha chars
			front=tolower(str[++f]);

		char back=tolower(str[b]); //normalize for upper vs lower comp
		while(!isalpha(back) && f>b)	//skip non alpha chars
			back=tolower(str[--b]);
		
		if(front!=back)
			return false;
		
	}

	return true;
}

- Anonymous August 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

all those f>b comparisons should be f<b...you win some you lose some ;-)

- Anonymous August 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Palindrome {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String str1="1sunilinus";
		
		if (str1.matches(".*[^a-zA-Z].*$") == true ){
			System.out.println("Not a valid string");
			return;
		}
			
		System.out.println("Is it Palindrome : "+isPalindrome(str1));

	}
	
	
	public static boolean isPalindrome(String s){
		char[] str = s.toCharArray();
		int len=str.length;
		
		System.out.println("Length of String: "+len);
		
		//loop
		int start = 0;
		int end = len-1;
		
		while(start<end)
		{
			if(str[start++] != str[end--])
				return(false);
		}
		
		return(true);
	}

}

- Sunil August 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in C# (Case and analphabetic charachters are ignored)

public static bool WordCheck(string word)
        {
            word = word.ToLower();
            string reversedWord = string.Empty;
            for (int i = word.Length - 1; i >= 0; i--)
            {
                if (Convert.ToInt32(word[i]) > 96 && Convert.ToInt32(word[i]) < 123)
                    reversedWord += word[i];
            }
            return reversedWord == word ? true : false;
        }

- Anonymous August 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in JavaScript:

var palindromeCheck = function(string) {

  var len = string.length - 1;
  var alpha = /^[a-zA-Z]+$/;

  for ( var i = 0, k = len; i < k; i++, k-- ) {
    while ( !alpha.test(string[i]) ) { i++; }
    while ( !alpha.test(string[k]) ) { k--; }
    if ( string[i] !== string[k] ) { return false; }
  }

  return true;
  
};

- adrice727 August 13, 2014 | 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