Goldman Sachs Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




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

Each thread that executes this code will get it's own parameter list and hence its own references to the passed in StringBuilder. From that perspective, the method call is 100% thread safe.

now. If 2 (or more) threads call this method passing in the **same** StringBuilder, then that same StringBuilder will be operated on in this method by each executing thread.

Since the StringBuilder is not in itself threadsafe, non-thread safe operations performed by each thread will collide.

- graviton6 December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Must be thread safe

- confused_banda September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

How this can be thread safe? StringBuffer is thread safe and StringBuilder not. Again the method is not even synchronized. So This is not thread safe

- Ghosh September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

As we are passing the stringbuilder as a parameter so every time method is called we will get the copy of string builder object...We are not doing anything extra in the method so in method each thread will have a different string builder object.

So we can say the method is threadsafe..Please correct if i m wrong..

- Dinesh September 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not threadsafe as StringBuilder reference can be changed by any other thread.

- java.interviews.questions September 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

Every thread will get its own copy of string builder object and java works as pass by value ...so reference will not get changed....Please correct if i am wrong

- Dinesh Pant September 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@dineshtrousers: You stand corrected.

- Anonymous September 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the object state is manipulated within the method then it's not thread-safe. However since we don't know if any such state is changed, we can say it's thread-safe for the same reasoning as Dinesh has mentioned above.

- MB November 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

This is 100% Thread safe

- Anonymous December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class StringBuilderThreadSafety {
    public static  void myMethodSafe(StringBuilder sb, String name){
        sb = new StringBuilder(sb.toString());
        sb.append(" ").append(name);
        System.out.println(sb.toString());
    }
    public static  void myMethodUnSafe(StringBuilder sb,String name){
        sb.append(" ").append(name);
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        final StringBuilder sb = new StringBuilder("foo: ");
        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            new Thread(){
                public void run(){
                    myMethodUnSafe(sb, ""+ finalI);
                }
            }.start();
        }
    }
}

It all depends what happens inside that method.
It could be either way.
Unsage output :
foo: 1 0
foo: 1 0
foo: 1 0 2
foo: 1 0 2 3
foo: 1 0 2 3 4
foo: 1 0 2 3 4 5
foo: 1 0 2 3 4 5 6
foo: 1 0 2 3 4 5 6 8
foo: 1 0 2 3 4 5 6 8 7
foo: 1 0 2 3 4 5 6 8 7 9

Safe Output:
foo: 0
foo: 6
foo: 4
foo: 5
foo: 3
foo: 2
foo: 1
foo: 7
foo: 8
foo: 9

- petko.ivanov September 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 6 vote

Since the StringBuilder is mutable object (unlike String) two threads can simultaneously modify it, therefore this method is not thread safe.

- srdjan September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right . This method is not thread safe, since two thread can pass same object and do the changes which will produce unwanted result.

- MrA October 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is thread safe..As there is nothing to be changed here by both these threads in this method.

- OTR December 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not thread safe

- Java123 October 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Immutable objects can have non thread-safe methods (No magics!)
Let's have a look at the following code.
view sourceprint?
01.
public class HelloAppender {
02.

03.
private final String greeting;
04.

05.
public HelloAppender(String name) {
06.
this.greeting = "hello " + name + "!\n";
07.
}
08.

09.
public void appendTo(Appendable app) throws IOException {
10.
app.append(greeting);
11.
}
12.
}
The class HelloAppender is definitely immutable. The method appendTo accepts an Appendable. Since an Appendable has no guarantee to be thread-safe (eg. StringBuilder), appending to this Appendable will cause problems in a multi-thread environment.

source:: java.dzone.com/articles/do-immutability-really-means

- Anonymous November 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

not thread safe because :-
1) it uses StringBuilder sb and in this same object can be passes and string builder is not thread safe. instead if it was string it would have been thread safe.

- Anonymous November 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

it is very well thread safe.provided if this object does not go out of this method.. if it will not go out of this method then it will be local object reference and each thread will have its own copy.. its no more a shared object...

- manikesh.verma September 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Rectify if I am wrong.
StringBuilder Object reference is passed as an argument. The whole StringBulder instance or Object is not cloned here. Only the value in the reference is "passed by value". For example

StringBuilder sb = new StringBuilder();//--- 1
obj.myMethod(sb);//--- 2

Say in Line 1, sb has a value "zzxccc" or whatever, which points to the memory location of this StringBuilder instance.
Now as line 2 is called, this "zzxccc" is copied and passed by value.
As a consequence, this sb which is local to the method, still refers to the same StringBuffer instance. How can the method be thread safe?

- banerjees36 September 20, 2013 | Flag


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