Amazon Interview Question for Software Engineer / Developers






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

public class MySingleton {
    
    
    //the static singleton object
    private static MySingleton theObject;

    private MySingleton() {
    }
    
    
    /**
     * Checks if the singleton object is created or not,
     * if not it creates the object and then the object is
     * returned.
     *
     * @return the singleton object
     */
    public static MySingleton createMySingleton() {
        
        if (theObject == null)
            theObject = new MySingleton();
        
        return theObject;
    }
}

- ano nee miss February 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MySingleton {
    
    
    //the static singleton object
    private static MySingleton theObject;
    
    /**
     * private constructor
     */
    private MySingleton() {
    }
    
    
    /**
     * Checks if the singleton object is created or not,
     * if not it creates the object and then the object is
     * returned.
     *
     * @return the singleton object
     */
    public static MySingleton createMySingleton() {
        
        if (theObject == null)
            theObject = new MySingleton();
        
        return theObject;
    }
}

- Anonymous February 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think u should also override the clone method of Object class to do nothing

- Sathya February 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is not thread safe.

- S February 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Not thread safe
2. Should be final, just to be extra cautious make abstract as well.

- Anonymous February 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Also an error, synchronization does not prevent
// two calls of constructor.
public static MySingleton getInstance() {
if (_instance==null) {
synchronized (MySingleton.class) {
_instance = new MySingleton();
}
}
return _instance;
}

In the correct solution, seen in Soultion 3, make getInstance() a synchronized method:

Solution 2
// Double-checked locking -- don't use
public static MySingleton getInstance() {
if (_instance==null) {
synchronized (MySingleton.class) {
if (_instance==null) {
_instance = new MySingleton();
}
}
}
}

Solution 3
// correct solution
public static synchronized MySingleton getInstance() {
// . . . continue as in Listing 3

- Sanjay Kumar (Inventwheel.com) February 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sanjay : Take care when u copy paste from other sites..
//...continue as in Listing 3
doesnt match ur listings..

- AB February 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MySingleton {
    
    
    //the static singleton object
    private static final MySingleton theObject = new MySingleton();
    
    /**
     * private constructor
     */
    private MySingleton() {
    }
    
    public MySingleton getInstance() { return theObject;}
 }

isn't that the simplest thread-safe way?

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

the getInstance() method has to be static.
Else how will you call that getInstance() method without an instance?

- Anonymous May 24, 2011 | 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