Goldman Sachs Interview Question for Java Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
17
of 19 vote

Time complexity O(n/2)

import java.util.*;
import java.io.*;

public class ispalindrome {
	public static boolean checkpalindrome(String str)
	{
		int n=str.length();
		boolean flag=true;
		for(int i=0;i<=n/2 && flag ==true;i++)
		{
			if(str.charAt(i) != str.charAt(n-i-1))
			{
				flag=false;
			}
		}
		return flag;
	}
	public static void main(String args[])
	{
		Scanner ip=new Scanner(System.in);
		String str = ip.nextLine();
		boolean flag = checkpalindrome(str);

		if(flag==true)
			System.out.print("palindrome");
		else
			System.out.println("Not a Palindrome");

	}

}

- Karan Tankshali November 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
6
of 8 vote

bool isPlaindrome(string str) {
	int n = str.length();
	for(int i = 0, j = n - 1; i < j; ++i, --j) {
		if(str[i] != str[j]) return false;
	}
        return true;
}

- Kaidul Islam October 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void panlindrome(String in){
        in = trim(in);
        for(int i=0, j=in.length()-1; i < j; i++, j--){
            if(in.charAt(i) != in.charAt(j)){
                System.out.println("False");
                return;
            }
        }
        System.out.println("True");
    }
    
    String trim(String in){
        in = in.toLowerCase();
        StringBuilder builder = new StringBuilder(in.trim().length());
        for(int i=0; i< in.length(); i++){
            char ch = in.charAt(i);
            if((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)){
                builder.append(ch);
            }
        }
        return builder.toString();
    }

- Sat October 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPalindrome(String str) {
        int start = 0;
        int end = str.length() - 1;

        while (start < end) {
            if (str.charAt(start) != str.charAt(end)) {
                return false;
            }
            start++;
            end--;
        }

        return true;

}

- IgorBrown31 October 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* compare ith char from 0 to i with (legnth -i)th char from length to length-i
* i = 0 to n/2 
* */
public boolean process(String s){
		char [] array = s.toCharArray();
		for(int i=0; i<(array.length)/2; i++){
			if(array[i] != array[array.length-i-1])
				return false;
		}
		
		return true;

- Julie October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void chkPalindrome(char strPalin[250])
{
	int len = strlen(strPalin);
	int j = len-1;
	for(int i=0;i<len/2;i++)
	{
		if(strPalin[i] == strPalin[j])
		{
			--j;
			continue;
		}
		else
		{
			cout << "not a palindrome";
			return;
		}
	}
	cout << "palindrome";
}

- sv October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

O(n) complexity and O(1) memory:

public static boolean isPalindrome(String str){
    //handle the invalid cases
    if(str == null){
        return false;
    }
    
    int lo = 0;
    int hi = str.length()-1;
    while(lo <= hi){
        if(str.charAt(lo) != str.charAt(hi)){
            return false;
        }
        lo++;
        hi--;
    }
    return true;
}

- Zortlord October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code for palindrome with space skipping and case insensitive

boolean isPalindrome(String strs) {
		if (strs == null) 
			return false;
		char[] str = strs.toCharArray();
		int i = 0;
		int j = str.length-1;
		while (i < j && str[i] == ' ') i++;
		while (j > i && str[j] == ' ') j--;
		while (i < j) {
			if (Character.toLowerCase(str[i++]) != 
							Character.toLowerCase(str[j--]))
				return false;
			while (i < j && str[i] == ' ') i++;
			while (j > i && str[j] == ' ') j--;
		}
		return true;
	}

- anonymous October 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
public static void main(String[] args) {
String str = "madam";
String s = "";
for(int i=0;i<str.length();i++)
{
s = s+str.charAt(i);
System.out.println(s);
}

if(str.equals(s))
{
System.out.println(str+" is palindrome");
}
else {
System.out.println(str+" is not palindrome");
}

}
}

- chinnaraj October 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CheckPalindromesInStringType {
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
System.out.println("Enter your String Type Data");
String name=sc.nextLine();
if(ispalindromes(name)){
System.out.println("is palindromes String");
}else{
System.out.println("is not palindromes String");
}


}


private static boolean ispalindromes(String name) {
int low=0;
int high=name.length()-1;
while (low<high) {
if(name.charAt(low) != name.charAt(high))
return false;
low++;
high--;
}
return true;
}

}

- Banwari Sharma October 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given a string as input, return true if it is a palindrome (ignoring capitalisation and punctuation marks). Look at test cases.

public class Palindrome {

	static public boolean isPalindrome(String in) {
		
		boolean result = false;
        if (in == null) return result;
        
		System.out.println(in);
		StringBuilder rightway = new StringBuilder();
		StringBuilder reverse  = new StringBuilder();
		
		for (int i=0; i<in.length(); i++) {
			char c = in.charAt(i);
			if (Character.isLetter(c)) {
				rightway.append(Character.toLowerCase(c));
				reverse.insert(0,Character.toLowerCase(c));
			}
		}
		
		result = rightway.toString().length() > 0 && rightway.toString().equals(reverse.toString());
		
		if ( result ) System.out.println("We have a palindrome");
		else          System.out.println("Not a palindrome");
		
		return result;
	}
	
	public static void main(String[] args) {

		isPalindrome("");
		isPalindrome("This isn't a palindrome");
		isPalindrome("Madam, I'm Adam");
		isPalindrome("Poor Dan is in a droop");
		
	}
}

- violethill March 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string s;
std::cin >> s;
if( equal(s.begin(), s.begin() + s.size()/2, s.rbegin()) )
std::cout << "is a palindrome.\n";
else
std::cout << "is NOT a palindrome.\n";
}

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

if( equal(s.begin(), s.begin() + s.size()/2, s.rbegin()) )
std::cout << "is a palindrome.\n";
else
std::cout << "is NOT a palindrome.\n";

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

#include<algorith>

equal(str.begin(), str.end(), str.rend());

- srdpt3 May 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string input;

cout << "Please enter a string: ";
cin >> input;

if (input == string(input.rbegin(), input.rend())) {
cout << input << " is a palindrome";
}

- srdpt3 May 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PalindromeString {

    public static void main(String... args) {
        PalindromeString ps = new PalindromeString();
        ps.testPalindrome("Able was I ere I saw Elba");
        ps.testPalindrome("Ze de Lima, Rua Laura, Mil e Dez");
        ps.testPalindrome("Dennis and Edna sinned");
        ps.testPalindrome("Was it a car or a cat I saw?");
        ps.testPalindrome("Madam, I'm Adam");
        ps.testPalindrome("I");
        ps.testPalindrome("OO");
        ps.testPalindrome("OIO");
        ps.testPalindrome("Hello");
        ps.testPalindrome("She");
    }

    private void testPalindrome(String testString) {
        System.out.println(String.format("The string '%s' %s palindrome.", testString, (this.isPalindrome(testString) ? "is" : "is not")));
    }

    private boolean isPalindrome(String string) {
        String normalString = normalize(string);
        int half = (int) Math.floor(normalString.length() / 2);
        for (int i=0; i<half; i++) {
            int j = normalString.length() - 1 - i;
            if (normalString.charAt(i) != normalString.charAt(j)) {
                return false;
            }
        }
        return true;
    }

    private String normalize(String string) {
        return string.toLowerCase().replaceAll("\\p{Punct}|\\s", "");
    }

}

- iz July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

One line answer

var result = Enumerable.SequenceEqual(text.ToCharArray(), text.ToCharArray().Reverse());

- Dr.Sai October 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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