Microsoft Interview Question for Software Engineer / Developers


Team: Global Foundation Services
Country: United States
Interview Type: In-Person




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

Double-checked locking can actually be incorrect, and is thus often considered an anti-pattern. Find the article on it on Wikipedia.

- eugene.yarovoi January 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

enums are implicitly thread safe as provided by VM in java, so this is the best way to define singleton.

- pawancse.16 April 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just use non-lazy load version:
class MyClass
{
private static MyClass _instance = new MyClass();
public MyClass GetInstance() {return _instance;}
}

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

The MyClass constructor should also be declared private for this class to be truly a singleton

- Anonymous February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The MyClass constructor should also be declared private for this class to be truly a singleton

- Anonymous February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The MyClass constructor should also be declared private for this class to be truly a singleton

- Anonymous February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have a readonly variable for the class instance this will make the singleton instance thread safe without any checking also the constructor should be private

- Anonymous February 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#ifndef SINGLETON_HPP
#define SINGLETON_HPP

#ifdef __GNUC__
#define HAS_THREAD_SAFE_STATICS 1
#endif

#if defined _MSC_VER
#include <intrin.h>
inline void MemoryWriteBarrier() { _WriteBarrier(); }
#elif __GNUC__
#if defined __i386__ || defined __x86_64__
inline void MemoryWriteBarrier() { __asm__ __volatile__("sfence" ::: "memory");
    }
#else
#endif

#ifdef HAS_THREAD_SAFE_STATICS

template <class T>
class SingletonBase
{
private:
    SingletonBase(const SingletonBase&);
    void operator=(const SingletonBase&);

protected:
    SingletonBase() {}
    ~SingletonBase() {}

public:
    static T& Instance()
    {
        static T instance;
        return instance;
    }

};

#else

template <typename T>
class SingletonBase
{
private:
    SingletonBase(const SingletonBase&);
    void operator=(const SingletonBase&);

protected:
    SingletonBase() {}
    ~SingletonBase() {}

public:
    static T& Instance()
    {
        if (!m_instance)
        {
            ScopedLocker<Mutex> lock(m_lock);
            if (!m_instance)
            {
                T* p = new T();
                MemoryWriteBarrier();
                m_instance = p;
                atexit(Destroy);
            }
        }
        return *m_instance;
    }
private:
    static void Destroy()
    {
        if (m_instance)
        {
            delete m_instance;
            m_instance = NULL;
        }
    }

private:
    static Mutex m_lock;
    static T* volatile m_instance;
};

template <typename T>
Mutex SingletonBase<T>::m_lock;

template <typename T>
T* volatile SingletonBase<T>::m_instance;

#endif // HAS_THREAD_SAFE_STATICS

template <typename T>
class Singleton : public SingletonBase<T>
{
};

#endif // SINGLETON_HPP

- seek January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can use double-checked locking mechanism, but we cannot use common mutex but memory barrier

- seek January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

try using the following code for singleton pattern that is other than double checking method
i,e
public static INSTANCE=new ClassName();// only one instance is created which and will be shared among all the classes

- Raj January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IODH:
class Singleton {
private Singleton(){ }

private static final Holder{
private static final Singleton instance = new Singleton();
}
public static final Singleton getInstance(){
return Singleton.Holder.instance;
}
}

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

Class Singleton{
int a;
int b;
...
}Singleton;
Use Singleton as object where ever you want to use, it will not all you to create a object as Singletion obj; as Singleton itself is an object. It also allows static vars like Singleton::a. Not sure if there any new compilers not supporting this, but I tested this in spro 3.4.

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

this is exactly correct

- ram January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java, simplest way to create singletion is using Enum.

public enum Singleton
          {
                     INSTANCE;
          }

- dileep.mandapam July 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Singelton
{
private:
static Singelton s_objSingelton;

protected:
Singelton()
~Singelton()

public:
static Singelton GetInstance()
{
return s_objSingelton;
}

};

- Designer August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Singleton{
 private static Singleton instance;
 private Singleton(){
 }
 public static Singleton getInstance(){
  if(Singleton.instance == null){
   Singleton.instance = new Singleton();
  }
  return (Singleton.instance == null)? (Singleton.instance = new Singleton()) : Singleton.instance; 
 }
}

- mika February 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

corrected code.

class Singleton{
 private static Singleton instance;
 private Singleton(){
 }
 public static Singleton getInstance(){
  return (Singleton.instance == null)? (Singleton.instance = new Singleton()) : Singleton.instance; 
 }
}

- mika February 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazySingleton =
        new Lazy<Singleton>(() => new Singleton());
    
    public static Singleton Instance { get { return lazySingleton .Value; } }

    private Singleton()
    {
    }
}

- shrihari.gct February 23, 2013 | 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