Microsoft Interview Question for abcs


Country: India
Interview Type: In-Person




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

I'm a noob at this, but it looks like they want to see some form of a singleton pattern. Here's my stab at it.

#include <iostream>

class ABC {

private:
	ABC() {
		std::cout << "ABC";
	}
	
	~ABC() {
		if (m_ABC != NULL)
			delete m_ABC;
	}
	
	
public:
	static void fun() {
		if (m_ABC == NULL) {
			// create an instance of ABC
			m_ABC = new ABC();
		}
	}
	
	
private:
	static ABC *m_ABC = NULL;

};

int main() {
	ABC::fun();
	return 0;
}

- Brendan June 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think they are looking for something like a singleton pattern...

#include <iostream>

class ABC {

private:
	ABC() {
		std::cout << "ABC";
	}
	
	~ABC() {
		if (m_ABC != NULL)
			delete m_ABC;
	}
	
	
public:
	static void fun() {
		if (m_ABC == NULL) {
			// create an instance of ABC
			m_ABC = new ABC();
		}
	}
	
	
private:
	static ABC *m_ABC = NULL;

};

int main() {
	ABC::fun();
	return 0;
}

- Brendan June 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ur code does not compile

- Anonymous June 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Define the function fun() as pure virtual function.

- var June 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A few solutions are proposed on cpluspluslearning-petert.blogspot.co.uk/2016/06/c-restrict-creation-of-objects.html.
The focus is on the process of creating and deleting objects.

- peter tang June 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Declare fun() as non static.
Then you can't make a call to fun() just by referring it via class name.
You have to call this function through object only instead of class like :-
ABC a; // statement 1
a.fun(); // statement 2

But as you the constructor is private and you can't make an instance, so
statement 1 would be illegal.

Also, still you could able to do it by using the below piece of code :-
ABC *a; // statement 1
a->fun(); // statement 2

As fun() is not making any reference to "this" object, so the above 2 statements were possible.

To overcome this, access some data from fun() as below :-

class A {

private:
int z;
A() { cout << " Constrcutor got called " << endl; }
~A() { cout << " destructor got called " << endl; }

public:
static void fun() { A aa; } // statement 1
void fun2() { A aa; } // Statement 2
void fun3() { z = 10; A aa; } // Statement 3

};

int main() {
A::fun();
A *a;
a->fun2();
a->fun3();

}

Now statement 3 will give segmentation fault and call to the constructor will be restricted in this way.

- kbkunalb September 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hi jkl,
I have Microsoft hackerrank round today. Can you please help me by telling what were the questions asked when u gave it?

- shreya June 25, 2016 | 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