Micron Interview Question for Software Engineers


Team: CMS
Country: United States




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

First, a small nitpick: it's Singleton, not Singelton, but this is probably not the sort of advice you're looking for.

There is nothing in your class that forces usage of the getInstance() method to get a MySingleton instance. Usually, it is considered good practice to make the constructor private, to prevent other code from inadvertently calling the constructor and obtain another instance. If this was part of an interview question, then I'd say this is what the interviewer was looking for - after all, if you really know what the Singleton pattern is all about, you should know that the code should *forbid* creation of other instances. You do it by making the constructor private.

- 010010.bin August 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is ok that the constructor should be private for single use of the object of the same class, but is there anything else related to the volatile?
Is it better to use multithreading concepts here? such as sync ?

- Anonymous August 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use double locking to avoid any concerns in case of multi threaded env.

- xyz August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agreed. Since the singleton is volatile, that should be fine. Not ideal.

- zortlord August 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's normally preferable to use a Static helper class to create Singletons. That way you can assured that the singleton instance was created. And this approach doesn't require the use of synchronization. Also, the code above doesn't declare the singleton as final.

public class MySingleton{

    private static class HELPER{
        private static final MySingleton instance = new MySingleton();
    }

    MySingleton s = HELPER.instance;
}

- zortlord August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are following problems:
* public constructor
* Not Thread safe
* If you synchronise the method the second question would come about whether synchronisation is needed at all, since once the instance is created there is no use of the method access to be synchronised. So you should be aware of double check locking implementation.
Having volatile makes sure non of the threads have a local dirty copy of the instance.

- mani.agnihotri August 25, 2015 | 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