Goldman Sachs Interview Question for Software Engineer / Developers


Country: United States




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

Swap first and last cells, second and second last cells, and so on till we reach the center

- bitflipper March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

Complexity would be O(n/2)

- calltorahul May 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

public class ReverseOfAString {

    public String reverse(String str) {
        char[] string = str.toCharArray();
        int len = string.length - 1;
        int middleElement = len / 2;

        for (int i = 0; i <= middleElement; i++) {
            char temp = string[i];
            string[i] = string[len - i];
            string[len - i] = temp;
        }

        return new String(string);

    }

    public static void main(String argc[]) {
        ReverseOfAString r = new ReverseOfAString();
        String rev = r.reverse("INDIA");
        System.out.println(rev);
    }
}

- AnanthaNag KUNDANALA April 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very good example

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

Depends on the language. It sounds like Java/C#, so it's immutable.

Use StringBuffer/StringBuilder.

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

complexity n for pushing all the strings, then for pop another n so total 2n Complexity

- Anonymous June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

is it not this simple???
public void reverseString()
{
String Input ="INDIA";
StringBuffer sbf = new StringBuffer();
for(int i=Input.length()-1;i>-1;i--)
{
sbf.append(Input.charAt(i));
}
System.out.println("Reverse of String: "+Input+" is : "+sbf.toString());
}

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

We are asked for a time efficient code, not a space efficient one. We can hence use another new string of same length as the given string. Then traverse the given string from right end and keep inserting the values in the new string in forward direction. Time complexity: O(n).

I don't know what they tested by asking this elementary question.

- ramblersm July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a stack...

- code jammer April 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* demoReverseString(char* str, int len)
{
len--;
int middleElem = len/2;
for(int ii = 0; ii < middleElem; ii++)
{
char temp = str[ii];
str[ii] = str[len-ii];
str[len-ii] = temp;
}
return str;
}

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

sorry there is a bug

char* demoReverseString(char* str, int len)
{
len--; // index starts from 0
int middleElem = len/2;
for(int ii = 0; ii <= middleElem; ii++)
{
char temp = str[ii];
str[ii] = str[len-ii];
str[len-ii] = temp;
}
return str;
}

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

public void reverseString(StringBuffer str)
{
int length=str.length()-1;
System.out.println(str.charAt(5));

for(int i=0, j=length;i<=j; i++, j--)
{
char ch=str.charAt(i);
str.setCharAt(i, str.charAt(j));
str.setCharAt(j, ch);

}

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

public void reverseString(StringBuffer str)
{
int length=str.length()-1;
System.out.println(str.charAt(5));

for(int i=0, j=length;i<=j; i++, j--)
{
char ch=str.charAt(i);
str.setCharAt(i, str.charAt(j));
str.setCharAt(j, ch);

}

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

public class Y{

void reverse(String q)
{

StringBuffer b =new StringBuffer(q);
int len=b.length();
char temp;
for(int i=0;i<=len/2;i++){
temp= b.charAt(i);
b.setCharAt(i, b.charAt(len-i-1)) ;
b.setCharAt(len-i-1, temp);

}
System.out.println(b);
}

public static void main(String args[]){

Y y =new Y();
y.reverse("ABCDEFGHI");

}
}

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

public static String reverse(char[] string, int index){
if(index == string.length){
return "";
}else {
return reverse(string, index +1) + string[index];
}
}

CALL LIKE
reverse("JAVA IS FUN".toCharArray(),0)

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

I believe this is one of the better way if we consider performance.

However we can still do it in recursive way without converting into char array:

public static String reverseStr(String str, int index) {
if (index == str.length()) {
return "";
} else {
return reverseStr(str, index + 1) + str.charAt(index);
}
}

and this has slightly better performance as compared to above one. Try with System.nanoTime() to see the difference.

- Ashu June 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What should be the function prototype to accept arguments like Fun_name("String");

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

Reverse 8/16/32 bytes with SSE or AXP using bit arithmetics (byte shuffling). If string space aligned on 64 bytes, but string length is not n*64, shift string pointer to avoid unaligned read/write.

- Mike October 25, 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