Expedia Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




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

public class TestPalindrome
{

public static void main(String[] args)
{
String newString = "Murder for a jar of red rum.";
String otherString = newString.replaceAll( "\\W", "" );
System.out.println(otherString);

String otherString2 = reverse2(otherString);
System.out.println(otherString2);

System.out.println(otherString);

if(otherString.equalsIgnoreCase(otherString2))
System.out.println("Palindrome");
else
System.out.println("not Palindrome");

}

public static String reverse2(String str)
{
char[] a = str.toCharArray();
int middle = str.length()/2;
int length = str.length() - 1;

for(int i = 0; i <= middle; i++)
{
char temp = a[i];
//System.out.println("temp is:" + temp);
//System.out.println("a[i] is:" + a[i]);
//System.out.println("a[length-i] is:" + a[length-i]);


a[i] = a[length-i];
a[length-i] = temp;
}

return new String(a);
}
}

- danqianchen April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There's no need to check the values at the Node-reference level. Instead, use the array offset, and look for symmetry:

static public boolean isPalin(String str)
	{
		for(int i=0;i<str.length()/2;i++)
		{
			if(str.charAt(i) != str.charAt(str.length()-1-i))
			{
				return false;
			}
		}
		
		return true;
	}

- Yev August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets assume the length of the string is given.
Compare the first and last characters, if equal, compare the second and second last characters, and so on.
If at any point comparison fails, return false.

bool is_palindrome(char* string,int length)
{
    int start = 0;
    int end = length;

    while(start<end)
    {
        if (string[start]!==string[end]) 
              return false;
        start++;end--;
    }
    return true;
}

- Anonymous March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

end = length - 1;

- Anonymous April 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

end = length / 2

- fdm April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is in Java

public class StringPalindromeOrNot {
    public static void main(String [] args) {
        String palindrome = "MALAYALAM";
        String notPalindrome = "MALADYLAM";
        boolean palindromeTrue = isPalindrome(palindrome);
        boolean palindromeFalse = isPalindrome(notPalindrome);
        System.out.println(palindrome + " = " + palindromeTrue);
        System.out.println(notPalindrome + " = " + palindromeFalse);
    }
    
    private static boolean isPalindrome(String word) {
        if(word == null || word.length() == 0) {
            throw new IllegalArgumentException("InvalidWord");
        }
        int front = 0;
        int back = word.length() - 1;
        while (front < back) {
            if(word.charAt(front) != word.charAt(back)) {
                return false;
            }
            else {
                front++;
                back--;
            }
        }
        return true;
    }
}

- nandkarthik March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Amazon asked this question on first round too.

- Sudo Man March 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isPalindrome(String str) 
{
    if (str == null || str.length() == 0)
        return false;

    for (int s = 0, e = str.length() - 1; s < e; ++s, --e) {
            if (str.charAt(s) != str.charAt(e) {
                return false;
    return true;   
}

- Anonymous April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code works. Please comment for any improvement.

public class IsStringPalindrome{
public static void main(String args[]){
try{
if(args[0]==null){
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("NOT");
System.exit(0);

}


if(isPalindrome(args[0]))
System.out.println("YES");
else
System.out.println("NOT");
}
public static boolean isPalindrome(String str){
System.out.println("str:" + str);
int i=0, j=(str.length()-1);
while(i<j){
if(str.charAt(i)!= str.charAt(j)){
System.out.println(str.charAt(i) + " " + str.charAt(j));
return false;
}
i++; j--;
}
return true;
}
}

- sreenivasulu May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {                                                  
     Scanner scan = new Scanner(System.in);                                                
                                                                                           
     String givenString = scan.nextLine();                                                 
                                                                                           
     String reverseString = reverseString(givenString);                                    
                                                                                           
     if (reverseString.equalsIgnoreCase(givenString)) {                                    
         System.out.println("Palindrome");                                                 
     } else {                                                                              
         System.out.println("Not Palindrome");                                             
     }                                                                                     
                                                                                           
                                                                                           
 }                                                                                         
                                                                                           
 private static String reverseString(String str) {                                         
                                                                                           
     char[] reversed = str.toCharArray();                                                  
                                                                                           
     for (int i = str.length() - 1, j = 0; i >= 0 && j < str.length()-1; i--, j++) {       
         reversed[j] = reversed[i];                                                        
     }                                                                                     
                                                                                           
     return String.valueOf(reversed);                                                      
 }

- Nayan July 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Solutions1
{
public static void main(String[] args)
{
String str = "Bob";
System.out.println(isPalin(str));
}

private static boolean isPalin(String str)
{
int i = 0;
int j = str.length() - 1;

while(i < j)
{
if(str.toLowerCase().charAt(i++) != str.toLowerCase().charAt(j--))
{
return false;
}
}
return true;
}
}

- Harsh Patel April 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Solutions1 
{
	public static void main(String[] args)
	{
		String str = "Bob";
		System.out.println(isPalin(str));
	}
	
	private static boolean isPalin(String str)
	{
		int i = 0; 
		int j = str.length() - 1;
		
		while(i < j)
		{
			if(str.toLowerCase().charAt(i++) != str.toLowerCase().charAt(j--))
			{
				return false;
			}
		}
		return true;
	}
}

- Harsh Patel April 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For checking palindrome of a string we need to traverse the mid of the string and keep checking that the first and the last characters of the string should not be unequal if they are then, return false else at the end of the string we will return true.

Implementation:

bool findpalindrome(string str){
	int i = 0;
	int j = str.length() - 1;
	while(i != j || i < j)
		if(str[i] != str[j])
			return false;
		i++;
		j--;
	}
	return true;
}

- swapnilkant11 July 23, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1.  String str = "ABCBA"
2. String revString = str.reverse();
3. return    (   (str.equals(revString) == true) ? true : false) ;

- Anonymous May 08, 2012 | 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