Amazon Interview Question for Software Engineer / Developers






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

I explained about the multithreading problem. Interviewer asked me to show the piece of the code in the singleton class that is not thread safe, then he asked how to solve the problem

- John June 23, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Singleton {

public: static Singleton *getInstance();
Singleton();
~Singleton();
Singleton(Singleton &another);

private: static Singleton *instance;

};

Singleton *Singleton::instance = 0;

Singleton *Singleton::getInstance() {

//here: obtain_lock for mutual exclusion.

if (!instance) {
instance = new Singleton();
return instance;
}
else
return instance;
}

the problem is that in multi-threaded environment, if two threads fight for a singleton object, we may have two threads with different data values. the singleton object itself exists as one, but the values within the object may be manipulated by the two threads by having its local copy and manupulation, etc...

Thus, a mutex problem. this can be solved by having lock mechnism at the location shown above.

- nunbit romance June 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the Singleton pattern constructor is usually declared as private - to not allow external instantiating of the class.

- Aleksey.M January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes without lock, the second process will cause the istance variable to be overwritten with the new singleton object created in heap. Memory leak.

- Noname June 29, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just for completion:

Also need to implement/override Clone method of the Object Class. This would avoid multiple copies of Singleton being available.

- Raghu July 10, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Singleton{

private static final objSingleton= new Singleton();

private Singleton(){
}

public static getInstance(){
return objSingleton;
}

...

}

this is no synchronization problem with this kind of singleton creation in java.

- Anonymous February 21, 2009 | 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